To perform a query with self-join on the following table:
> select * from test_users
id email
--- -----------
1 "a@abc.com"
2 "a@abc.com"
3 "b@abc.com"
I can use SQL:
> select u1.id u1id, u2.id u2id from test_users u1 inner join test_users u2 on u1.email=u2.email and u1.id !=u2.id
u1id u2id
----- ------
1 2
2 1
Question:
- How can I write this in Django ORM?
- If I want to remove the duplicate so that I get only 1 row in above example, how can I achieve that in Django?