(クロスカラム)重複する電話番号を持つすべての行を検索するには:
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
);
2つの列で重複するすべての電話番号を検索するには:
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;
同じ行の両方の列で同じ番号を繰り返す 資格がありません。私はあなたがそれらを含めたくないと思います。 (それでも、CHECK
で許可しない価値のあるノイズになります 制約。)