How to explode user id from a database column?

前端 未结 2 528
囚心锁ツ
囚心锁ツ 2021-01-28 07:25

I have a table name messages. There is a column name receiver_id I need to fetch record where receiver_id = 4 but i am always getting null output.<

相关标签:
2条回答
  • 2021-01-28 07:51

    This should also work (SQL Fiddle):

    SELECT * 
    FROM messages
    WHERE receiver_id LIKE '%,4,%'
       OR receiver_id LIKE '4,%'
       OR receiver_id LIKE '%,4'
       OR receiver_id = '4'
    

    Or with regex (SQL Fiddle):

    SELECT * 
    FROM messages
    WHERE receiver_id REGEXP '4,|,4,|,4'
       OR receiver_id = '4'
    
    0 讨论(0)
  • 2021-01-28 07:53

    Storing comma separated values is really a bad design you should normalize it first by storing all association of receiver in a junction table,If you are not able to alter your schema then for your current situation you can use find_in_set() to search values in a comma separated list

    select * from table 
    where find_in_set(4,receiver_id ) >0
    
    0 讨论(0)
提交回复
热议问题