单个MySql查询获取喜欢我喜欢的帖子的用户列表

时间:2014-04-27 04:43:46

标签: mysql

MySql表:likes

+----+---------+--------------+
| id | post_id | liked_by_uid |
+----+---------+--------------+
|  1 |   10    | 56           |
|  2 |   10    | 51           |
|  3 |   11    | 51           |
|  4 |   11    | 57           |
|  5 |   12    | 58           |
|  6 |   13    | 58           |
+----+---------+--------------+
  1. 我的用户ID(uid)是51
  2. 有4个帖子(post_id 10,11,12,13)
  3. 我确实喜欢发布10和11(likes_by_uid是51)
  4. 我想要uid(56,57)的列表,因为他们喜欢我喜欢的帖子(10,11)。
  5. 我可以使用两个查询

    SELECT post_id FROM likes WHERE liked_by_uid='51' //51 is me
    

    $ ids =“'10','11'”;从第一个查询获得

    SELECT liked_by_uid FROM likes WHERE post_id IN($ids);
    

    期望的uids(56,57)来自第二次查询

    如何使用单一查询。

1 个答案:

答案 0 :(得分:0)

SELECT liked_by_uid FROM likes WHERE post_id IN
    (SELECT post_id FROM likes WHERE liked_by_uid='51')