mysql | Request from empty column and FIND_IN_SET

£可爱£侵袭症+ 提交于 2019-12-20 07:05:06

问题


I have a table:

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

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` ) 

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.


回答1:


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

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

you need to do it like that:

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

DEMO HERE



来源:https://stackoverflow.com/questions/22480418/mysql-request-from-empty-column-and-find-in-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!