0

我在所有三个表上都有三个表 t1、t2 和 t2,它们的字段名称相同,例如

  1. ID
  2. 家庭电话
  3. 工作电话
  4. 移动电话

我想使用 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 != '')

我在这里可能做错了什么?

4

3 回答 3

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 返回每个表中的所有记录,并且不会消除重复项。只是 Union 会删除重复项。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)

这意味着 ID 4 在表 1 和表 2 中都可以找到 ID 5 在表 1 和表 3 中都可以找到。
但重复项是根据所有 4 个字段 MATCHING ID + 3 个电话确定的。如果 ID 不是重复项的一部分,它可以从 select 和 group by 中删除,作为 group_Concat(ID) 添加,并且与 tablesDupIn 结合会告诉您所涉及的 ID 和表。根据您打算如何处理结果,这可能是有益的,也可能不是......

于 2013-04-13T10:36:45.120 回答
0

您可以在这些之间使用OR而不是AND

   AND t3.HomePhone IS NOT NULL 
   AND t3.HomePhone != ''

像那样

    AND (t3.HomePhone IS NOT NULL 
         OR t3.HomePhone != '')

在您的所有查询中使用 OR 更改

于 2013-04-13T10:28:30.003 回答
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)
于 2013-04-13T10:31:00.103 回答