棘手的SQL查询:加入,分组,拥有

时间:2018-04-19 18:53:23

标签: mysql sql group-by count having

我有以下SQL测试表;见SQLFiddle here

> 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)的方法。

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 |
+-------------------------+-----------------+

缺少什么: 我所有的电影都被同一评论家评了两次,但是我不知道如何过滤最新评论比以前更多的明星。

如果有人能指出我在这里正确的方向,我将非常感激。堆栈溢出对我来说非常有用:)

编辑:添加我的尝试和缺少的内容。

1 个答案:

答案 0 :(得分:0)

这应该为你提供你想要的数字1,对于数字2,我需要一个sqlfiddle中的数据来玩。在此期间,我建议您查看lag,但firstlast的某些组合可能会为您提供所需内容。 *请注意,这不会给你完整的答案,它只是一个参考。

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;