mysql |来自空列和 FIND_IN_SET 的请求

2022-01-05 00:00:00 request php mysql find-in-set

我有一张桌子:

id  name   position   status
1   A      1,2        1
2   B      1          1
3   C                 1
4   D      2          1

其中:position 列是一个文本字段;我的请求在这里:

Where: position column is a text field; My request is here:

SELECT `id` 
FROM  `table` 
WHERE `status`=1
AND `position` >  ''
AND `position` = FIND_IN_SET( 1,  `position` ) 
OR  `position` = FIND_IN_SET( 2,  `position` ) 

此请求将返回:1,2,3,4.这是我需要的错误:1,2,4 ->条件:(position > '').哪里有问题以及如何更改我的请求?谢谢.

This request will return: 1,2,3,4. This is a wrong as I need: 1,2,4 -> Condition: (position > ''). Where is a problem and how to change my request? Thanks.

推荐答案

您在检查字段列表中的数字时无需检查位置是否为空.

you dont need to check if position is empty while you checking numbers in field list .

你不需要检查 position = FIND_IN_SET.... .它将返回 1 所在位置的值.

you dont need to check position = FIND_IN_SET.... . it will return the value where 1 is in position.

你需要这样做:

 SELECT `id` 
 FROM  `table` 
 WHERE `status`=1
 AND  FIND_IN_SET( 1,  `position` ) 
 OR   FIND_IN_SET( 2,  `position` ) 

此处演示

相关文章