In Postgres I have a table as below:
Table phones
|id | phone |
| 1 | +12345678 |
| 2 | +95687445 |
| 3 | +78945646 |
| 4 | +12345678 |
As you can see id 1 and 4 have the same phone number.
I want to get all rows which has same phone number but different id like this:
| id1 | id2 | phone |
| 1 | 4 | +12345678 |
I have tried to use JOIN with itself but that returns duplicate results:
select
p1.id as id1,
p2.id as id2,
p1.phone
from phones as p1
join phones as p2
on p1.phone = p2.phone
| id1 | id2 | phone |
| 1 | 4 | +12345678 |
| 4 | 1 | +12345678 |