我有三个表包含电影,评级和评论员数据。我的数据连接如下所示:
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 |
+-------------------------+-------+------------------+
不知怎的,我被困在如何做那件事上了。有人能给我指出正确的方向吗?
编辑:我最初认为在stars列中包含另一个聚合函数( MAX(stars)
)很琐碎,但我不能让它工作。有什么办法吗?
4条答案
按热度按时间u4dcyp6a1#
下面的代码将对您有所帮助,
6tqwzwtp2#
wztqucjr3#
rekjcdws4#
解决方案1:-从结果集中:
第二个解决方案:-从你的table