我有一张带有卡号和姓名的表。其中一些名称是相同的,这意味着他们有多张牌。如何选择名称多次出现的所有行?
答案 0 :(得分:5)
以下可能是您正在寻找的解决方案
Select * from CardTable where cardid in ( select cardid from CardTable group by cardid having count(cardid) > 1)
答案 1 :(得分:2)
使用:
SELECT t.name
FROM TABLE t
GROUP BY t.name
HAVING COUNT(*) > 1
答案 2 :(得分:1)
以下内容应该有效:
SELECT *
FROM cards c1
INNER JOIN cards c2 on c1.name = c2.name
WHERE c2.id <> c1.id
答案 3 :(得分:0)
select count(*) as n, [name]
from table1
group by [name]
having n > 1
答案 4 :(得分:0)
如果您需要整行,这里有一个可以在sql 7及更高版本上运行的解决方案
SELECT t1.* FROM(
SELECT t.name
FROM YourTable t
GROUP BY t.name
HAVING COUNT(*) > 1) t2
JOIN YourTable t1 on t2.name = t1.name