仅返回使用MySQL DISTINCT出现一定次数的字段

时间:2012-06-16 04:05:04

标签: mysql

我使用以下查询返回多次出现在表格中的艺术家的名字,第一个字符以A开头

SELECT DISTINCT artist FROM releases WHERE artist LIKE 'a%' ORDER BY artist

我现在想要更进一步,并希望只返回出现超过10次的艺术家

我该怎么做?

干杯!

3 个答案:

答案 0 :(得分:3)

执行此操作的最佳方法是使用GROUP BY获取艺术家出现的次数,然后使用HAVING表示他们需要至少出现一定次数。

我相信这个查询会给你你想要的东西:

SELECT artist, count(*) 
FROM releases 
WHERE artist LIKE 'a%'
GROUP BY artist 
ORDER BY artist
HAVING count(*) > 10

答案 1 :(得分:1)

SELECT artist, count(*)  FROM releases 
WHERE artist LIKE 'a%' GROUP BY artist
ORDER BY artist
HAVING count(*) > 10 

答案 2 :(得分:0)

您的查询选择只有艺术家名称:

select artist from releases
where artist like 'a%' group by artist 
having count(distinct(artist))>10;