Search code examples
phpmysqlrequestfind-in-set

mysql | Request from empty column and FIND_IN_SET


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.


Solution

  • 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