我有两个表,如下所示,它们有重复的记录。我需要获取两个表中存在的记录,重复行和表2中的重复数量
Table 1
A B C D E
192 168 101 100 32
192 168 101 100 32
192 168 101 102 30
192 168 101 102 30
表2
A B C D E
192 168 101 100 32
192 168 101 100 32
192 168 101 100 32
192 168 101 102 30
例如,我需要获取记录192 168 101 100 32,因为它在表2中的出现比表1更多。我知道如何从各个表中获取计数但是如何将count与每个元组的两个表进行比较
select A,B,C,D, count(*) from table1 inner join table 2 on table1.A=table2.A and table1.B=table2.B and table1.c=table2.c and table1.d=table2.d group by A,B,C,D having count(table2.A,table2.B,table2.C,table2.D)>count(table1.A,table1.B,table1.c,table.D)
上面的查询在语法上是不正确的,但我想做类似的事情
答案 0 :(得分:0)
尝试此查询:
SELECT t1.A, t1.B, t1.C, t1.D, t1.E
FROM (
SELECT A,B,C,D,E, count(*) AS cnt
FROM table1
GROUP BY A,B,C,D,E
) t1
INNER JOIN (
SELECT A,B,C,D,E, count(*) AS cnt
FROM table2
GROUP BY A,B,C,D,E
) t2 ON t1.A = t2.A AND t1.B = t2.B AND t1.C = t2.C AND t1.D = t2.D AND t1.E = t2.E
WHERE t1.cnt < t2.cnt