mysql通过有来计数组
我有这张表:
Movies (ID, Genre)
一部电影可以有多种类型,因此 ID 并不特定于某个类型,而是多对多的关系。我想要一个查询来查找恰好有 4 种类型的电影总数。我当前的查询是
SELECT COUNT(*)
FROM Movies
GROUP BY ID
HAVING COUNT(Genre) = 4
但是,这会返回 4 的列表而不是总和。如何获取总和而不是 count(*)
列表?
I have this table:
Movies (ID, Genre)
A movie can have multiple genres, so an ID is not specific to a genre, it is a many to many relationship. I want a query to find the total number of movies which have at exactly 4 genres. The current query I have is
SELECT COUNT(*)
FROM Movies
GROUP BY ID
HAVING COUNT(Genre) = 4
However, this returns me a list of 4's instead of the total sum. How do I get the sum total sum instead of a list of count(*)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种方法是使用嵌套查询:
内部查询获取恰好具有 4 个流派的所有电影,然后外部查询计算内部查询返回的行数。
One way would be to use a nested query:
The inner query gets all the movies that have exactly 4 genres, then outer query counts how many rows the inner query returned.
也许
虽然这不是所有电影的总和,但有多少电影有 4 种类型。
所以也许你想要
Maybe
although that would not be the sum of all the movies, just how many have 4 genre's.
So maybe you want
怎么样:
What about: