MySQL WHERE IN ()

2021-11-20 00:00:00 mysql

我的查询是:

SELECT * FROM table WHERE id IN (1,2,3,4);

我将它用于用户组,并且一个用户可以在多个组中.但似乎当一条记录具有多个 id 时,例如 1 和 3,mySQL 不会返回该行.

I use it for usergroups and a user can be in more than one group. but it seems that when a record has multiple id like 1 and 3, mySQL does not return that row.

有什么办法也可以得到那一行吗?

Is there any way to get that row too?

推荐答案

您的查询转化为

SELECT * FROM table WHERE id='1' or id='2' or id='3' or id='4';

它只会返回与其匹配的结果.

It will only return the results that match it.

避免复杂性的一种解决方法是将数据类型更改为SET.然后你可以使用,FIND_IN_SET

One way of solving it avoiding the complexity would be, chaning the datatype to SET. Then you could use, FIND_IN_SET

SELECT * FROM table WHERE FIND_IN_SET('1', id);

相关文章