Untuk menemukan semua baris dengan nomor telepon duplikat (lintas kolom):
SELECT *
FROM contacts c
WHERE EXISTS (
SELECT FROM contacts x
WHERE x.mobile_phone IN (c.mobile_phone, c.home_phone)
OR x.home_phone IN (c.mobile_phone, c.home_phone)
AND x.contact_id <> c.contact_id -- except self
);
Untuk menemukan semua nomor telepon duplikat di dua kolom:
SELECT DISTINCT phone
FROM (
SELECT mobile_phone AS phone
FROM contacts c
WHERE EXISTS (
SELECT FROM mobile_phone x
WHERE c.mobile_phone IN (x.mobile_phone, x.home_phone)
AND c.contact_id <> x.contact_id -- except self
)
UNION ALL
SELECT home_phone
FROM contacts c
WHERE EXISTS (
SELECT FROM mobile_phone x
WHERE c.home_phone = x.home_phone -- cross-over covered by 1s SELECT
AND c.contact_id <> x.contact_id -- except self
)
) sub;
Mengulangi angka yang sama di kedua kolom dari baris yang sama tidak memenuhi syarat. Saya tidak berpikir Anda ingin memasukkan itu. (Masih akan menjadi suara bising yang mungkin perlu dilarang dengan CHECK
kendala.)