【发布时间】:2018-04-19 10:04:36
【问题描述】:
我有三个包含电影、评分和评论者数据的表格。我的数据的连接如下所示:
mysql root@localhost:foo> SELECT m.title, ra.stars, re.name
-> FROM Movie m
-> JOIN Rating ra ON m.mid = ra.mid
-> JOIN `Reviewer` re ON ra.rid = re.rid
-> ORDER BY m.title, ra.stars DESC;
+-------------------------+-------+------------------+
| title | stars | name |
+-------------------------+-------+------------------+
| Avatar | 5 | James Cameron |
| Avatar | 3 | Elizabeth Thomas |
| E.T. | 3 | Ashley White |
| E.T. | 2 | Chris Jackson |
| Gone with the Wind | 4 | Sarah Martinez |
| Gone with the Wind | 3 | Mike Anderson |
| Gone with the Wind | 2 | Sarah Martinez |
| Raiders of the Lost Ark | 4 | Brittany Harris |
| Raiders of the Lost Ark | 4 | Chris Jackson |
| Raiders of the Lost Ark | 2 | Brittany Harris |
| Snow White | 5 | Elizabeth Thomas |
| Snow White | 4 | Daniel Lewis |
| The Sound of Music | 3 | Chris Jackson |
| The Sound of Music | 2 | Brittany Harris |
+-------------------------+-------+------------------+
我想要实现的是按标题对上面的表格进行分组,并获得给每部电影最高评分的评论者的姓名。结果应如下所示。
+-------------------------+-------+------------------+
| title | stars | name |
+-------------------------+-------+------------------+
| Avatar | 5 | James Cameron |
| E.T. | 3 | Ashley White |
| Gone with the Wind | 4 | Sarah Martinez |
| Raiders of the Lost Ark | 4 | Brittany Harris |
| Raiders of the Lost Ark | 4 | Chris Jackson |
| Snow White | 5 | Elizabeth Thomas |
| The Sound of Music | 3 | Chris Jackson |
+-------------------------+-------+------------------+
我不知如何做到这一点。谁能指出我正确的方向?
编辑:我最初虽然在星列 (MAX(stars)) 上包含另一个聚合函数是微不足道的,但我无法让它工作。任何想法如何工作?
【问题讨论】:
-
试试我的答案。希望对你有帮助。