I am wishing to compare two sets of data using SQL wherever possible. Please consider the following data structures:
tbl_users:
+---------+----------+
| user_id | avatar |
+---------+----------+
| 1 | test.jpg |
| 2 | 1234.jpg |
+---------+----------+
tbl_shortlists:
+------+--------+
| id | owner |
+------+--------+
| 1 | 1 |
| 2 | 2 |
+------+--------+
tbl_shortlist_items:
+---------+--------------+
| item_id | shortlist_id |
+---------+--------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 1 | 2 |
| 2 | 2 |
+---------+--------------+
I wish to select the tbl_users.user_id and tbl_users.avatar where the shortlist owned by any user contains two or more of the same item_id as the current owner. So, let’s assume I am looking up the user_id 1, with the above data structure. Since the Shortlist with the ID shares two items with shortlist_id 1 (i.e. user 1’s shortlist), I would like to return user_id = 2 and avatar = 1234.jpg.
I am at a loss as to how to do this in pure SQL. I was thinking that it might be possible to use IN(), but I don’t know if that would work.
Here’s some pseudo-code to hopefully explain a little better what I would like:
Select user_id and avatar
for each shortlist that contains two or more item_ids that are in the shortlist
that is owned by user_id = 1.
As always with SQL it helps to break it down into small sections. i.e. the first thing you need is all the items in short lists that the current user owns:
Next you need all users who have short lists with the same items in. For explanatory purposes I will build up as an
INstatement, but an INNER JOIN may perform better.You then need to limit this to those that have 2 or more, by adding a
GROUP BYandHAVINGYou then need to use
tbl_usersto get the avatar for these ownersAs I said, I think rearranging this to JOINs will be optimised better, but have not tested this theory.