Skip to content
Advertisement

How to find people who likes each other?

I’m using mysql. There is likes_table for users. However, I am stuck to find people who like each other.

likes_table:

For example, I want to find mutual likes for from_user_id =1 Therefore expected result should be:

I tried to connect two columns with IN command.

SQL code I tried:

This gives only user_id=1

Advertisement

Answer

One method is to filter the table for two conditions:

  • Row where the from_user_id = 1; and,
  • The reciprocal row exists.

The second condition can be handled using exists:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement