首页 > 解决方案 > 如何检查一条记录在不同表中重复了多少次

问题描述

我这里有两张桌子:

表格1:

process_id customer_id
16         1
21         1
22         1

表 2:

process_id customer_id
16         1
16         1
22         1

我想检查表 1 中的每一行在表 2 中重复了多少次。

例如,表 1 中的第 1 行在表 2 中重复 2 次,第 2 行重复 0 次,第 3 行重复 1 次。我不确定如何遍历表 1 中的每一行并得到这个结果。

标签: sql

解决方案


据我了解,这就是您所要求的:

select table1.process_id,table1.customer_id,count(table2.process_id) as table2count
from table1 left outer join table2 on table1.process_id==table2.process_id and table1.customer_id=table2.customer_id
group by table1.process_id,table1.customer_id;

推荐阅读