2

How can I build a query to do the following?

  1. Grouping by phonenumber
  2. Where That Grouping has over 5 different names

data fields: id, phonenumber, name

I've tried:

SELECT count(phonenumber), phonenumber, GROUP_CONCAT(name)
FROM list
group by phonenumber
having count(phonenumber) > 5

but it's timing out, so not sure if that's the right format.

fake data:

id | phonenumber | name
1  | 555-5555    | joe
2  | 444-4444    | joe
3  | 555-5555    | joe
4  | 555-5555    | dan
5  | 555-5555    | Ben
6  | 555-5555    | Griff
7  | 555-5555    | Sam
8  | 555-5555    | Jim

desired output:

count(phonenumber | phonenumber | GROUP
7                 | 555-5555    | joe, dan, Ben, Griff, Sam, Jim
4

1 回答 1

1

看起来您的查询工作正常。这是sqlfiddle

如果您只需要获取DISTINCT名称,那么您可以这样做

SELECT COUNT(phonenumber) AS `count`, 
       phonenumber, 
       GROUP_CONCAT(DISTINCT name) AS `names`
  FROM `list` 
 GROUP BY phonenumber 
HAVING COUNT(phonenumber) > 5

这是sqlfiddle

于 2013-02-13T21:40:54.570 回答