如何按此逗号分隔数据进行分组

时间:2011-08-22 10:46:47

标签: mysql

我有这样的数据:

ID  Book    Author
1   Book A  Andy, Brian
2   Book B  Andy, Charlie
3   Book C  Brian

如何按数据分组,以便我可以这样:

Author  Count
Andy    2
Brian   2
Charlie 1

1 个答案:

答案 0 :(得分:0)

如果我们假设您有一个标准化结构,您只需要:

SELECT a.author, count(*) 
FROM books b
INNER JOIN author a ON a.author_id=b.author_id
GROUP BY a.author