我有一个包含以下架构的表:
CUSTOMERS (id INT, name VARCHAR(10), height VARCHAR(10), weight INT)
id
是主键。我想找出具有完全相同名称,相同高度和相同重量的人的行。换句话说,我希望找到与name
,height
和weight
相关的重复项。
示例表
1, sam, 160, 100
2, ron, 167, 88
3, john, 150, 90
4, sam, 160, 100
5, rick, 158, 110
6, john, 150, 90
7, sam, 166, 110
示例输出:
现在因为有同名,相同身高和相同体重的人:
sam (id=1), sam (id=4)
和
john (id=3), john (id=6)
我想得到这些ID。如果我每次匹配只得到一个id
也是可以的(即第一次匹配时id = 1,第二次匹配时id = 3)。
我正在尝试此查询,但不确定它是否正确。
SELECT id
FROM customers
GROUP BY name, height, weight
答案 0 :(得分:2)
SELECT *
FROM customers C
INNER JOIN
(
SELECT name, height, weight
FROM customers
GROUP BY name, height, weight
HAVING COUNT(*) > 1
) X ON C.name = X.name AND C.height = X.height AND C.weight = X.weight
答案 1 :(得分:2)
试试这个(对sql server有效):
SELECT
t.NAME,
'Ids = '+
(
SELECT cast(Id as varchar)+','
FROM Customers c
WHERE c.NAME = t.NAME AND c.Weight = t.Weight AND c.Height = t.Height
FOR XML PATH('')
)
FROM
(
SELECT Name, height, weight
FROM Customers
GROUP BY Name, height, weight
HAVING COUNT(*) > 1
) t
或强>
正如您所问 - 每场比赛只有一个ID
SELECT
t.NAME,
c.Id
FROM
(
SELECT Name, height, weight
FROM Customers
GROUP BY Name, height, weight
HAVING COUNT(*) > 1
) t
JOIN Customers c ON t.NAME AND c.Weight = t.Weight AND c.Height = t.Height
答案 2 :(得分:1)
你是正确的方式:
SELECT min(id)
FROM customers
GROUP BY name, height, weight
HAVING COUNT(*) > 1
答案 3 :(得分:1)
SELECT c.*
FROM customers c
JOIN (
SELECT name, height, weight
FROM
GROUP BY name, height, weight
HAVING count(*) > 1
) t ON c.name = t.name and c.height = t.height and c.weight = t.weight
答案 4 :(得分:1)
由于您标记了多个数据库,我不知道您在使用什么。
在Sql server中,如果没有将其放入SELECT
,您将无法选择ID。
因此,如果您想要选择除分组中的其他字段,则可以使用PARTITION BY
。像这样:
SELECT id,
ROW_NUMBER() OVER(PARTITION BY c.name, c.height, c.weight ORDER BY c.name) AS DuplicateCount
FROM customers c
这将为您提供具有相同名称,身高和体重的重复项的ID 我不确定这比其他解决方案更快,但是,你可以对其进行分析并进行比较。
答案 5 :(得分:0)
如果你说的每场比赛只能获得一个id,你就接近解决方案了:
SELECT
min( id )
,name, height, weight --<-- oncly if you need/want
FROM customers
GROUP BY name, height, weight
HAVING count(*) > 1