我有一张大表包含20,000条记录。我想找到类似的记录(那些BNO和BNO-CSCcode列上的重复记录)
CSCcode Description BNO BNO-CSCcode EID
05078 blah1 5430 5430-05078 1098
05026 blah2 5431 5431-05026 1077
05026 blah3 5431 5431-05026 3011
04020 blah4 8580 8580-04020 3000
07620 blah5 7560 7560-07620 7890
07620 blah6 7560 7560-07620 8560
05020 blah1 5560 5560-04020 1056
01234 sampledesc 0009 0009-01234 1156
04567 sampledesc2 0056 0056-04567 1656
01234 sampledesc8 0009 0009-01234 0023
我想要检索
CSCcode Description BNO BNO-CSCcode EID
05026 blah2 5431 5431-05026 1077
05026 blah3 5431 5431-05026 3011
07620 blah5 7560 7560-07620 7890
07620 blah6 7560 7560-07620 8560
01234 sampledesc 0009 0009-01234 1156
01234 sampledesc8 0009 0009-01234 0023
如何在sql查询中指定。
答案 0 :(得分:1)
select t1.*
from table_name t1
inner join (select BNO,BNO-CSCcode
from table_name
group BNO,BNO-CSCcode
having count(1)>1) as t2
on t1.BNO=t2.BNO and t1.BNO-CSCcode=t2.BNO-CSCcode
答案 1 :(得分:0)
select field1,field2,field3, count(*)
from table_name
group by field1,field2,field3
having count(*) > 1
答案 2 :(得分:0)
使用HAVING:
select *
from mytable
where bno & bno_csccode in
(
select bno & bno_csccode
from mytable
group by bno, bno_csccode
having count(*) > 1
);
答案 3 :(得分:0)
您必须在查询中使用EXISTS
命令。
这将提供所需的结果:
SELECT *
FROM TableName t0
WHERE EXISTS (
SELECT 1
FROM TableName t1
WHERE t0.BNO = t1.BNO AND t0.BNOCSC = t1.BNOCSC
GROUP BY BNO, BNOCSC
HAVING count(*) > 1
)
EXISTS
用于比较一个或多个列。