-2

如果有这两个表:

漫画

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

评分

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. 同样在这里我将如何计算按下后退按钮时漫画的最高平均评分。

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

4

2 回答 2

1

这是您开始的事情,尽管您的问题很可能会被关闭....该示例基于通常的场景,因为我们无法获取太多信息来帮助您解决问题...

这是有关如何在 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 的视觉表示

在此处输入图像描述

于 2013-01-25T11:51:19.513 回答
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)
于 2013-01-25T12:10:56.053 回答