I have two tables.
users
| id | name |
| ---|--------|
| 1 | Jhon |
| 2 | David |
| 3 | Sweety |
swipes
| id | swp_from | swp_to| first_swp| second_swp |
| ---|----------|-------|----------|------------|
| 1 | 1 | 2 | like | pending |
| 2 | 3 | 1 | like | pending |
I have the following query:
SELECT u.name, u.gender, s.swp_to, s.first_swp FROM swipes s
LEFT JOIN users u ON
CASE
WHEN s.swp_from = :me THEN u.id = s.swp_to AND ((s.first_swp NOT IN ('like', 'superlike')) OR (s.first_swp IN ('like', 'superlike') AND s.second_swp NOT IN ('like', 'superlike')))
WHEN s.swp_to = :me THEN u.id = s.swp_from AND ((s.first_swp NOT IN ('like', 'superlike') AND s.second_swp != 'pending') OR (s.first_swp IN ('like', 'superlike') AND s.second_swp NOT IN ('like', 'superlike', 'pending')))
END
The above query does give expected result but it is displaying same result for the other user as well. I want to restrict displaying the results only for :me
user. The user :me
is the logged in user for whom the results are being fetched for. The problem here with this query is that though it returns correct expected results for :me
user BUT it also returns the same result for the other user as in the query as well. This should not be the case. The other user is either swp_from
if :me
user is :swp_to
OR the other user is swp_to
if :me
user is swp_from
.
EXAMPLE (UPDATE):
Let's say for example, :me
is logged in user with id 1 i.e., John. In swipes
table first row, 1 is swp_from
and 2 is swp_to
then I need to display David's (user id 2) data to John. Similarly, in second row, 1 is in swp_to
this time so I need to display Sweety's (user id 3) data to John. This means that no matter the logged in user :me
i.e., John (user id 1) is in either swp_from
or swp_to
, I need to display the other user's data to John. That's it. However, it is also displaying John's data to both David and Sweety with the query above. That is what I not want. I am writing query to display the corresponding user's data for John only (i.e., for the logged in user :me
only).