Mysql search with comma delimited string

前端 未结 3 641
花落未央
花落未央 2020-12-21 19:56

I have 1 mysql table called colors with rows id and name

1 - yellow
2 - black
3 - red
4 - green
5 - white
6 - bl         


        
3条回答
  •  猫巷女王i
    2020-12-21 20:34

    http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

    select id from tab where find_in_set(name, '$colors') > 0
    

    NB: as per Dan's comment below, this query doesn't use indexes and will be slow on a large table. A query with IN is better:

    select id from tab where name IN ('blue', 'red', 'white')
    

提交回复
热议问题