我在所有三个表上都有三个表 t1、t2 和 t2,它们的字段名称相同,例如
- ID
- 家庭电话
- 工作电话
- 移动电话
我想使用 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 != '')
我在这里可能做错了什么?