PHP + MySQL WHERE EXISTS find_in_set返回所有值

时间:2016-08-06 14:43:07

标签: php mysql

我有以下表结构:

uc_users

id  |  following | Name
------------------------
1   |  2,3,      | Bill
2   |  1,3,      | Bob
3   |  1,        | Dan

我想知道用户ID是谁。

我做了以下事情:

SELECT p.*
FROM `uc_users` p
WHERE EXISTS (SELECT 1
          FROM `uc_users`
          WHERE `id` = 1 AND find_in_set(id, following) > 0
         )
ORDER BY id DESC
LIMIT 20;

但它给了我所有3个用户,它应该只给用户2和3。

有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT p.*
FROM `uc_users` p
WHERE EXISTS (SELECT 1
          FROM `uc_users`
          WHERE `id` = 1 AND find_in_set(p.id, following) > 0
         )
ORDER BY p.id DESC
LIMIT 20;

更改: FIND_IN_SET(p.id,following) > 0

存储分隔列表太糟糕了。相反,您应该存储每个<id,follower>对的记录。

Is storing a delimited list in a database column really that bad?