這裏是東西給你下手,但最有可能您的問題將被關閉....樣本是基於通常的情況下,因爲沒有太多的信息,我們可以搶來幫助你從你的問題了.. 。
這裏是關於How to post a question on SO的尖端。
* SQLFIDDLE DEMO
樣品嚐試:
select c.comic_id, c.comic_title,
COUNT(r.comic_id), avg(r.ratings)
from comics c
left join rating r
on r.comic_id = c.comic_id
group by c.comic_id
;
| COMIC_ID | COMIC_TITLE | COUNT(R.COMIC_ID) | AVG(R.RATINGS) |
---------------------------------------------------------------
| 100 | a | 3 | 5 |
| 200 | b | 4 | 6.5 |
| 300 | c | 3 | 5.6667 |
| 400 | d | 2 | 8 |
分鐘平均:
select x.comic_id, x.comic_title,
min(average) from (
select c.comic_id, c.comic_title,
COUNT(r.comic_id), avg(r.ratings) average
from comics c
left join rating r
on r.comic_id = c.comic_id
group by c.comic_id) x
;
| COMIC_ID | COMIC_TITLE | MIN(AVERAGE) |
-----------------------------------------
| 100 | a | 5 |
編輯AS PER OP的評論:
OP要評級的最低評級,排名第二低ð等..最高等級,第二位..等
這個查詢將使用variable
排名。
* SQLFIDDLE DEMO
查詢:
select x.comic_id, x.comic_title,
x.average from (
select (@rank:[email protected]+1) as rank, c.comic_id, c.comic_title,
COUNT(r.comic_id), avg(r.ratings) average
from (select @rank:=0) rk, comics c
left join
rating r
on r.comic_id = c.comic_id
group by c.comic_id
order by average asc) x
where x.rank = 1
;
| COMIC_ID | COMIC_TITLE | AVERAGE |
------------------------------------
| 100 | a | 5 |
爲了熟悉JOIN,你可以看看這篇文章:VISUAL REPRESENTATION OF SQL JOINS
請正確解釋自己。這是一個非常令人困惑的問題 – cjds
聽起來不錯。當你陷入某處時,繼續回到這裏 –
SQL和按鈕在一個問題中沒有多大意義 – Bulat