这是您开始的事情,尽管您的问题很可能会被关闭....该示例基于通常的场景,因为我们无法获取太多信息来帮助您解决问题...
这是有关如何在 SO 上发布问题的提示。
* SQLFIDDLE 演示
样品尝试:
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 |
根据 OP 的评论进行编辑:
OP想要排名最低,第二低等等..最高等级,第二高..等等
此查询将使用 avariable
进行排名。
* SQLFIDDLE 演示
询问:
select x.comic_id, x.comic_title,
x.average from (
select (@rank:=@rank+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,你可以看看这篇文章:SQL JOIN 的视觉表示