Mysql WHERE problem with comma-separated list

前端 未结 3 996
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 19:48

I need help for this problem.

In MYSQL Table i have a field :

Field  : artist_list  
Values : 1,5,3,401

I need to find all records

3条回答
  •  粉色の甜心
    2020-12-11 20:29

    Short Term Solution

    Use the FIND_IN_SET function:

    SELECT uid 
      FROM tbl 
     WHERE FIND_IN_SET('401', artist_list) > 0
    

    Long Term Solution

    Normalize your data - this appears to be a many-to-many relationship already involving two tables. The comma separated list needs to be turned into a table of it's own:

    ARTIST_LIST

    • artist_id (primary key, foreign key to ARTIST)
    • uid (primary key, foreign key to TBL)

提交回复
热议问题