mysql count group by having(mysql 计数组)
问题描述
我有这张桌子:
Movies (ID, Genre)
一部电影可以有多种类型,因此 ID 不是特定于一种类型,而是一种多对多的关系.我想要一个查询来查找恰好有 4 种类型的电影总数.我当前的查询是
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
然而,这会返回一个 4 的列表而不是总和.如何获得总和而不是 count(*) 的列表?
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(*)?
推荐答案
一种方法是使用嵌套查询:
One way would be to use a nested query:
SELECT count(*)
FROM (
SELECT COUNT(Genre) AS count
FROM movies
GROUP BY ID
HAVING (count = 4)
) AS x
内部查询获取恰好有 4 种类型的所有电影,然后外部查询计算内部查询返回的行数.
The inner query gets all the movies that have exactly 4 genres, then outer query counts how many rows the inner query returned.
这篇关于mysql 计数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mysql 计数组
基础教程推荐
- oracle区分大小写的原因? 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
