表数据如上图,
筛选 type、pid 重复的数据的次数大于等于2的 次数和对应的数据值
SELECT COUNT(*),TYPE,pid FROM AREA GROUP BY TYPE,pid HAVING COUNT(*)>=2;
筛选 type、pid 重复的数据的次数大于等于2,并且对应的 pid和type值相反的重复的数据的次数大于等于2,且一次对应算1条有效数据,例如筛选出 type=0 and pid=1 重复的数据次数大于等于2 和type=1 and pid=0 重复的数据次数大于等于2 的数据算一条有效数据。
SELECT COUNT(*)/2 FROM
(SELECT s1.type st1,s1.pid sp1, s2.type st2, s2.pid sp2 FROM(
SELECT TYPE,pid FROM AREA GROUP BY TYPE,pid HAVING COUNT(*)>=2)s1 ,
(SELECT TYPE,pid FROM AREA GROUP BY TYPE,pid HAVING COUNT(*)>=2)s2) ss
WHERE st1 = sp2 AND sp1 = st2;
来源地址:https://blog.csdn.net/yuanyedage/article/details/126375824