棘手的sql查询:连接、分组、拥有

w1jd8yoj  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(378)

我有一个sql测试的下表;看这里的小提琴

> SELECT * FROM `Movie`;
+-----+-------------------------+------+------------------+
| mID | title                   | year | director         |
+-----+-------------------------+------+------------------+
| 101 | Gone with the Wind      | 1939 | Victor Fleming   |
| 102 | Star Wars               | 1977 | George Lucas     |
| 103 | The Sound of Music      | 1965 | Robert Wise      |
| 104 | E.T.                    | 1982 | Steven Spielberg |
| 105 | Titanic                 | 1997 | James Cameron    |
| 106 | Snow White              | 1937 | <null>           |
| 107 | Avatar                  | 2009 | James Cameron    |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+-----+-------------------------+------+------------------+
> SELECT * FROM Rating;
+-----+-----+-------+------------+
| rID | mID | stars | ratingDate |
+-----+-----+-------+------------+
| 201 | 101 | 2     | 2012-01-22 |
| 201 | 101 | 4     | 2013-01-27 |
| 202 | 106 | 4     | <null>     |
| 203 | 103 | 2     | 2008-01-20 |
| 203 | 108 | 4     | 2002-01-12 |
| 203 | 108 | 2     | 2009-01-30 |
| 204 | 101 | 3     | 2010-01-09 |
| 205 | 103 | 3     | 2010-01-27 |
| 205 | 104 | 2     | 2010-01-22 |
| 205 | 108 | 4     | <null>     |
| 206 | 107 | 3     | 2013-01-15 |
| 206 | 106 | 5     | 2014-01-19 |
| 207 | 107 | 5     | 2000-01-20 |
| 208 | 104 | 3     | 1999-01-02 |
+-----+-----+-------+------------+
> SELECT * FROM Reviewer;
+-----+------------------+
| rID | name             |
+-----+------------------+
| 201 | Sarah Martinez   |
| 202 | Daniel Lewis     |
| 203 | Brittany Harris  |
| 204 | Mike Anderson    |
| 205 | Chris Jackson    |
| 206 | Elizabeth Thomas |
| 207 | James Cameron    |
| 208 | Ashley White     |
+-----+------------------+

我已经解决了所有的问题,除了这两个:
1.)对于每部至少有一个评级的电影,请查找电影标题和明星总数、最高明星和给出最高明星的人。
我得到的是:

SELECT m.title, ra.stars, re.name
FROM Movie m
JOIN(
    SELECT R.*
    FROM Rating R
    JOIN(
        SELECT mid, MAX(stars) AS Stars
        FROM Rating
        GROUP BY mid
    ) D ON R.mid = D.mid AND R.Stars = D.Stars
) Ra ON m.mid = ra.mid
JOIN Reviewer re ON ra.rid = re.rid;
+-------------------------+-------+------------------+
| title                   | stars | name             |
+-------------------------+-------+------------------+
| Gone with the Wind      | 4     | Sarah Martinez   |
| Raiders of the Lost Ark | 4     | Brittany Harris  |
| The Sound of Music      | 3     | Chris Jackson    |
| Raiders of the Lost Ark | 4     | Chris Jackson    |
| Snow White              | 5     | Elizabeth Thomas |
| Avatar                  | 5     | James Cameron    |
| E.T.                    | 3     | Ashley White     |
+-------------------------+-------+------------------+

缺少的是:我找不到一个方法来补充 SUM(stars) 每部电影到table上。
2.)对于同一评论员对同一部电影评分两次,第二次评分较高的所有情况,请返回评论员的姓名和电影的标题。
到目前为止我得到的:

SELECT title, name
FROM Movie m
JOIN Rating ra ON m.mid = ra.mid
JOIN Reviewer re ON ra.rid = re.rid
GROUP BY title, name
HAVING COUNT(*) > 1;
+-------------------------+-----------------+
| title                   | name            |
+-------------------------+-----------------+
| Gone with the Wind      | Sarah Martinez  |
| Raiders of the Lost Ark | Brittany Harris |
+-------------------------+-----------------+

缺少的是:我有所有的电影,是由同一评论员两次评级,但我不知道如何筛选的情况下,最新的审查有更多的明星比早些时候。
如果有人能给我指出正确的方向,我将不胜感激。堆栈溢出今天对我非常有帮助:)
编辑:添加我的尝试和缺少的内容。

mepcadol

mepcadol1#

这应该会给你你想要的数字1,数字2,我需要在一个sqlfiddle数据玩。同时,我建议 lag 尽管 first 以及 last 可能会给你想要的*注意这不会给你确切的答案,它只是一个参考。

select mov.title, sum(rat.stars), max(rat.stars), rev.name
from Movie mov,
  Rating rat,
  Reviewer rev
where mov.mid = rat.mid
and rat.rid = rev.rid
group by mov.title;

相关问题