skip to Main Content

I have this records:

Number
1, 2
1, 24

How to select only 2 by LIKE not 2 and 24

SELECT * FROM test WHERE number LIKE '%2%'

1, 2 // need only this record

1, 24

3

Answers


  1. find_in_set almost does what you want, but you’ll have to remove the spaces in order to use it:

    SELECT * 
    FROM   test
    WHERE  FIND_IN_SET('2', REPLACE(number, ' ', '')) > 0
    
    Login or Signup to reply.
  2. You should avoiding storing unnormalized CSV data like this. That being said, if you must proceed, here is one way:

    SELECT * 
    FROM test
    WHERE CONCAT(' ', number, ' ') LIKE '% 2 %';
    
    Login or Signup to reply.
  3. You can do it as follows :

    SELECT `number`
    FROM `test` 
    WHERE TRIM(SUBSTRING_INDEX(`number`, ',', -1)) = 2 or TRIM(SUBSTRING_INDEX(`number`, ',', 1)) = 2;
    

    SUBSTRING_INDEX to split number, and TRIM to remove any space, then we search in to parts of the number.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search