GROUP_CONCAT - WHERE条件

时间:2017-09-14 11:00:13

标签: mysql aggregate-functions

select group_concat(bookgenre), author_authorID
from Written
group by author_authorID;

给了我

group_conat(bookgenre)      author_authorID
Fiction                     123450
Fiction, History            123451
History                     123457
Sci-Fi                      123458
Fiction                     123459

我不知道如何只生成

group_concat(bookgenre)     author_authorID
Fiction                     123450
Fiction                     123459

当我添加条件

where "group_concat(bookgenre)" like 'Fiction'

结果返回0行

2 个答案:

答案 0 :(得分:0)

Group_concat是一个聚合函数,因此您应该使用having而不是where来过滤 对于喜欢你应该使用wildchar %

select group_concat(bookgenre), author_authorID
from Written
group by author_authorID;
havine group_concat(bookgenre) LIKE '%fiction%'

答案 1 :(得分:0)

你可以通过

这样做
select group_concat(bookgenre), author_authorID
from Written
group by author_authorID;
having count(*) = sum(bookgenre = 'Fiction')

或使用having子句来过滤聚合函数的结果

select group_concat(bookgenre), author_authorID
from Written
group by author_authorID;
having group_concat(bookgenre) = 'Fiction'