mysql 数据表多重计算_MySQL查询:单表多重比较

这是一个找到整个表的匹配对的解决方案 - 您可以根据需要添加where子句进行过滤。基本上它基于相等的“成员”和不相等的“id”进行自我加入。然后,它比较由2个ID分组的结果计数,并将它们与原始表中的那些ID的总计数进行比较。如果它们都匹配,则意味着它们具有相同的确切成员。

select

t1.id, t2.id

from

table t1

inner join table t2

on t1.member = t2.member

and t1.id < t2.id

inner join (select id, count(1) as cnt from table group by id) c1

on t1.id = c1.id

inner join (select id, count(1) as cnt from table group by id) c2

on t2.id = c2.id

group by

t1.id, t2.id, c1.cnt, c2.cnt

having

count(1) = c1.cnt

and count(1) = c2.cnt

order by

t1.id, t2.id这是我使用的一些样本数据,返回(1,3)和(6,7)的匹配

insert into table

values

(1, 'abc'), (1, 'pqr'), (2, 'xyz'), (3, 'pqr'), (3, 'abc'), (4, 'abc'), (5, 'pqr'),

(6, 'abc'), (6, 'def'), (6, 'ghi'), (7, 'abc'), (7, 'def'), (7, 'ghi')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值