2017-09-14 125 views
2
select group_concat(bookgenre), author_authorID 
from Written 
group by author_authorID; 

給我GROUP_CONCAT - WHERE條件

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行返回

回答

0

使用您可以通過

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

或使用具備子句來過濾聚合函數的結果

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

感謝這些工作完美 - 非常感謝 –

0

GROUP_CONCAT是一個聚合函數,所以你應該過濾使用具有和不到哪
而對於像你應該wildchar %

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

完美 - 歡呼 –

+0

以及如果我的回答或其他是正確的,請選擇一個標記它被接受...看到這裏如何 http://meta.stackexchange.com/questions/5234/how-does-accepting-一個回答工作 – scaisEdge