I have a table "users":
drop table users;
CREATE TABLE users(
id int,
user_id int,
phone_number VARCHAR(30),
email VARCHAR(30));
INSERT INTO users
VALUES
(1, 999, 61412308310, '[email protected] '),
(2, 129, 61477708777, '[email protected] '),
(3, 213, 61488908495, '[email protected]'),
(4, 145, 61477708777, '[email protected]'),
(5, 214, 61421445777, '[email protected]'),
(6, 214, 61421445326, '[email protected]');
I want to select all rows that have duplicate user_id or duplicate phone_number or duplicate email.
result should be:
2, 129, 61477708777, '[email protected] '
4, 145, 61477708777, '[email protected]'
5, 214, 61421445777, '[email protected]'
6, 214, 61421445326, '[email protected]'
id = 2 and id = 4 match the search terms (phone_number = 61477708777). id = 5 has the same email with row id 4, id=6 has the same user_id with id=5.
2
Answers
Problem with joining the table with itself multiple times using different conditions.
try:
This query first filters the rows based on the input user_id, then it checks for any matching email addresses. You can add
union
for the phone_number as well.fiddle
Recursive query is what you need. It helps you express declaratively the reasoning of adding another rows for given seed row:
fiddle