我有三个表t1,t2和t2,所有三个表都有相同的字段名称,例如
我想用t1检查t2和t3中的事件。我提出了MySQL查询(下面),但是当我运行它时我得到一个空集,知道我确实在所有三个表中都有重复的电话号码:
SELECT t1.id, t1.HomePhone, t1.WorkPhone, t1.MobilePhone,
(
SELECT COUNT(*)
FROM t2
WHERE t1.HomePhone = t2.HomePhone
AND
(t2.HomePhone IS NOT NULL OR t2.HomePhone != '')
UNION
SELECT COUNT(*)
FROM t2
WHERE t1.WorkPhone = t2.WorkPhone
AND
(t2.WorkPhone IS NOT NULL OR t2.WorkPhone != '')
UNION
SELECT COUNT(*)
FROM t2
WHERE t1.MobilePhone = t2.MobilePhone
AND
(t2.MobilePhone IS NOT NULL OR MobilePhone != '')
) AS Countt2,
(
SELECT COUNT(*)
FROM t3
WHERE t1.HomePhone = t3.HomePhone
AND
(t3.HomePhone IS NOT NULL OR t3.HomePhone != '')
UNION
SELECT COUNT(*)
FROM t3
WHERE t1.WorkPhone = t3.WorkPhone
AND
(t3.WorkPhone IS NOT NULL OR t3.WorkPhone != '')
UNION
SELECT COUNT(*)
FROM t3
WHERE t1.MobilePhone = t3.MobilePhone
AND
(t3.MobilePhone IS NOT NULL OR MobilePhone != '')
) AS Countt3
FROM t1
WHERE
(t1.HomePhone IS NOT NULL OR t1.HomePhone != '')
AND (t1.WorkPhone IS NOT NULL OR t1.WorkPhone != '')
AND (t1.MobilePhone IS NOT NULL OR t1.MobilePhone != '')
我在这里做错了什么?
答案 0 :(得分:1)
前提:复制被定义为所有4个字段MATCH。因此,通过联合所有表并“计算它们> 1然后您将识别重复项。而groupConcat列出具有重复的表。
SELECT count(B.src), B.ID, B.HomePhone, B.WorkPHone, B.MobilePHone,
group_Concat(B.SRC) as TablesDupIN
FROM (
(SELECT 't1' as SRC, id, HomePhone, WorkPhone, MobilePhone FROM T1)
UNION ALL
(SELECT 'T2', id, HomePhone, WorkPhone, MobilePhone FROM T2)
UNION ALL
(SELECT 'T3', id, HomePhone, WorkPhone, MobilePhone FROM T3)) B
GROUP BY ID, HomePhone, WorkPhone, MobilePhone
HAVING count(*) > 1
union all返回每个表中的所有记录,但不会消除重复项。只是联盟会删除重复。 t1,t2,t3成一个临时表。我们可以从ID,HomePHone,WorkPhone和MobilePhone的临时表分组中进行选择。假设需要所有4个字段来确定重复。如果不是这种情况,您可以删除字段ID,如果它只是所有必须相同的电话。通过对结果进行分组,我们将其组合为ID,家庭电话,工作电话和移动电话等行。我使用'T1','T2'来识别具有重复的表格,副本在ID上,以及TalesDupIn中列出的表格中的3个电话号码。
2 4 555-5555 555-5555 555-5555 (t1,t2)
2 5 666-6666 666-6666 666-6666 (t1,t3)
3 7 777-7777 777-7777 777-7777 (t1,t2,t3)
2 8 888-8888 888-8888 888-8888 (t2,t3)
这意味着在表1和表2中都找到了ID 4 ID 5见表1和表3 但副本是根据所有4个字段MATCHING ID + 3手机确定的。 IF ID不是副本的一部分,它可以从select和group by中删除,作为group_Concat(ID)添加,并且与tablesDupIn结合将告诉您所涉及的ID和表。根据您打算如何处理结果,这可能有益或无效......
答案 1 :(得分:0)
您可以在这些
之间使用OR
代替AND
AND t3.HomePhone IS NOT NULL
AND t3.HomePhone != ''
像那样
AND (t3.HomePhone IS NOT NULL
OR t3.HomePhone != '')
在所有查询中使用OR进行更改
答案 2 :(得分:0)
SELECT
COUNT(DISTINCT t1.id)
COUNT(DISTINCT t2.id)
COUNT(DISTINCT t3.id)
FROM t1
LEFT JOIN t2 ON
(t1.HomePhone=t2.HomePhone AND t1.HomePhone IS nOT NULL)
OR (t1.WorkPhone=t2.WorkPhone AND t1.WorkPhone IS nOT NULL)
OR (t1.MobilePhone=t2.MobilePhone AND t1.MobilePhone IS nOT NULL)
LEFT JOIN t3 ON
(t1.HomePhone=t3.HomePhone AND t1.HomePhone IS nOT NULL)
OR (t1.WorkPhone=t3.WorkPhone AND t1.WorkPhone IS nOT NULL)
OR (t1.MobilePhone=t3.MobilePhone AND t1.MobilePhone IS nOT NULL)