计算按下一个上一个按钮时显示的平均评级

时间:2013-01-25 11:18:51

标签: mysql sql left-join

如果有这两个表:

COMICS

Field          Type                   Comment
comic_id       bigint(10)             unsigned NOT NULL
comic_title    varchar(500)           NULL

RATING

Field               Type               Comment
rating_id           bigint(10)         unsigned NOT NULL
comic_id            varchar(250)       NULL
rating              varchar(250)       NULL

现在我想创建一个API,以便在Next&的新闻中返回评价最高的漫画。代表评分的Previous按钮。

  1. 默认情况下,API返回评分最高的漫画。然后按下一个按钮如何计算最低平均评级漫画

  2. 同样如何按下“返回”按钮计算漫画的最高平均等级。

  3. 简而言之,我想创建一个Next previous Button that will show avg rating from rating table on behalf of comic id

2 个答案:

答案 0 :(得分:1)

这是你开始的事情,虽然很可能你的问题将被关闭....样本基于一般情况,因为没有太多的信息我们可以抓住你的问题来帮助你...

以下是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 |

根据OP评论编辑:

OP希望排名最低评级,第二低评级等等。最高评级,第二高等等。

此查询将使用variable排名。

* SQLFIDDLE DEMO

查询:

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,您可以查看一下这篇文章:VISUAL REPRESENTATION OF SQL JOINS

enter image description here

答案 1 :(得分:0)

创建一个视图以使其更容易:(更新为包括没有评级的漫画)

CREATE VIEW avg_comic_rating AS 
SELECT 
    comics.comic_id, comics.comic_title, COALESCE(AVG(comic_rating), 0.0)AS avg_rating
 FROM
    comics LEFT JOIN comic_rating
ON   (comics.comic_id = comic_rating.comic_id)
GROUP BY comics.comic_id;

然后使用限制偏移进行分页:

select * from avg_comic_rating order by avg_rating desc, comic_title asc limit 0,2;
+----------+-------------+------------+
| comic_id | comic_title | avg_rating |
+----------+-------------+------------+
|        6 | zorro       |     7.5000 |
|        5 | super man   |     7.0000 |
+----------+-------------+------------+
2 rows in set (0.05 sec)


select * from avg_comic_rating order by avg_rating desc, comic_title asc limit 2,2;
+----------+-------------+------------+
| comic_id | comic_title | avg_rating |
+----------+-------------+------------+
|        4 | cat woman   |     4.5000 |
|        2 | he man      |     4.5000 |
+----------+-------------+------------+
2 rows in set (0.00 sec)

表格内容供参考:

select * from comics;
+----------+-------------+
| comic_id | comic_title |
+----------+-------------+
|        1 | batman      |
|        2 | he man      |
|        3 | she man     |
|        4 | cat woman   |
|        5 | super man   |
|        6 | zorro       |
+----------+-------------+
6 rows in set (0.00 sec)

select * from comic_rating;
+-----------+----------+--------------+
| rating_id | comic_id | comic_rating |
+-----------+----------+--------------+
|         1 |        1 |            2 |
|         2 |        2 |            3 |
|         3 |        3 |            1 |
|         4 |        4 |            4 |
|         5 |        5 |            6 |
|         6 |        6 |            5 |
|         7 |        1 |            5 |
|         8 |        2 |            6 |
|         9 |        3 |            7 |
|        10 |        4 |            5 |
|        11 |        5 |            8 |
|        12 |        6 |           10 |
+-----------+----------+--------------+
12 rows in set (0.00 sec)

select * from avg_comic_rating;
+----------+-------------+------------+
| comic_id | comic_title | avg_rating |
+----------+-------------+------------+
|        1 | batman      |     3.5000 |
|        2 | he man      |     4.5000 |
|        3 | she man     |     4.0000 |
|        4 | cat woman   |     4.5000 |
|        5 | super man   |     7.0000 |
|        6 | zorro       |     7.5000 |
+----------+-------------+------------+
6 rows in set (0.00 sec)