我有一个名字数据库,有些名字没有以正确的方式插入。 SecondName有时被输入为FirstName。
int* (*function)(int, (int (*k)(int *)));
我尝试了不同的分组查询
+-----------------+--------------+
| FirstName | SecondName |
+-----------------+--------------+
| Bob | Smith |
| Gary | Rose |
| John | Jones |
| Smith | Bob |
| Gary | Oberstein |
| Adam | Sorbet |
| Jones | John |
+-----------------+--------------+
但我无法得到任何东西
select `FirstName`
, `SecondName`
from `names`
where ( `FirstName`
, `SecondName` )
in ( select `FirstName`
, `SecondName`
from `names`
group
by `FirstName`
, `SecondName`
having count(*) > 1
)
答案 0 :(得分:4)
有一个技巧可以做到这一点,你需要规范你的名字,快速的方法是你将名字和姓氏按字母顺序排列,然后对结果进行分组。
SELECT name_normalized, count(*) as c
FROM (
SELECT CASE WHEN FIRSTNAME < LASTNAME THEN FIRSTNAME||LASTNAME
ELSE LASTNAME|| FIRSTNAME END as name_normalized
FROM names
) X
GROUP BY name_normalized
注意:
答案 1 :(得分:2)
如果COUNT()
本身并不重要,您可以轻松地将重复项与INNER JOIN
SELECT n.FirstName, n.SecondName, n2.FirstName, n2.SecondName
FROM Names n
INNER JOIN Names n2 on n.FirstName = n2.SecondName and n.SecondName = n2.FirstName