gpt4 book ai didi

mysql - 编写 HAVING 条件的最有效方法

转载 作者:行者123 更新时间:2023-11-29 05:17:23 31 4
gpt4 key购买 nike

我目前正在完成 More JOIN operations sqlzoo 的教程,遇到了下面的代码作为#12 的答案:

SELECT yr,COUNT(title) 
FROM movie
JOIN casting
JOIN actor
ON actorid=actor.id AND movie.id=movieid
WHERE name='John Travolta'
GROUP BY yr

HAVING COUNT(title)=
(SELECT MAX(c) FROM
(SELECT yr, COUNT(title) AS c
FROM movie
JOIN casting ON movie.id=movieid
JOIN actor ON actorid=actor.id
WHERE name='John Travolta'
GROUP BY yr) AS t
)

难道没有更简洁的方式来表达这段代码吗?

最佳答案

是的。如果我正确理解了这个问题,那就简单多了。

SELECT yr,COUNT(title) 
FROM movie JOIN casting ON movie.id=movieid
JOIN
actor ON actorid=actor.id
WHERE name='John Travolta'
GROUP BY yr
HAVING COUNT(title) > 2

关于mysql - 编写 HAVING 条件的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30923148/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com