[英]how to select years which has more than 2 movies released in mysql
My code is :我的代码是:
SELECT count(*), title, release_year
FROM film
GROUP BY release_year
HAVING COUNT(title) > 2;`
but I got the error: #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sakila.film.title但我收到错误: #2 of SELECT 列表不在 GROUP BY 子句中,并且包含非聚合列 'sakila.film.title
which is not functionally dependent on columns in GROUP BY clause;它在功能上不依赖于 GROUP BY 子句中的列; this is incompatible with sql_mode=only_full_group_by 0.038 sec
这与 sql_mode=only_full_group_by 0.038 秒不兼容
You want to either group by both title and release_year:您想按 title 和 release_year 分组:
SELECT count(*), title, release_year
FROM film
GROUP BY title, release_year
HAVING COUNT(title) > 2;
or just release_year:或只是 release_year:
SELECT count(*), release_year
FROM film
GROUP BY release_year
HAVING COUNT(title) > 2;
possible adding GROUP_CONCAT(title)
to get all titles in for that group in a single row.可能添加
GROUP_CONCAT(title)
以在一行中获取该组的所有标题。
Remove title
.删除
title
。 If you are using a group keywords for x
column, only use x
column name and sql functions like count, min, max, etc in select statement.如果您对
x
列使用组关键字,则在select语句中仅使用x
列名和sql 函数,如 count、min、max 等。
To show all tiles the have a count bigger than 2 you can use GROUP _cONCAT and you will not get your error any more要显示计数大于 2 的所有图块,您可以使用 GROUP _cONCAT 并且您不会再收到错误消息
SELECT count(*), GROUP_CONCAT(title SEPARATOR ';'), release_year
FROM film
GROUP BY release_year
HAVING COUNT(title) > 2;`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.