difference between where_in and find_in_set

后端 未结 3 1705
暗喜
暗喜 2021-01-24 18:09

I am working on a join query in which I have to get the data from a column which contain the comma separated values.like allowed_activity contain 1,2,3,4,5,6<

3条回答
  •  北海茫月
    2021-01-24 19:06

    WHERE IN requires the set of values to be specified literally in the query, not as a single value containing a comma-delimited string. If you write:

    WHERE 6 IN (a.allowed_activity)
    

    it will treat a.allowed_activity as just a single value, and compare it with 6, not as a set of multiple values to search.

    FIND_IN_SET searches a comma-delimited string for the value.

    Another way to view it is that IN is a shortcut for a bunch of = tests combined with OR:

    WHERE x IN (a, b, c, d)
    

    is short for

    WHERE x = a OR x = b OR x = c OR x = d
    

    When you rewrite it like this, you can see clearly why it won't work with a column containing a comma-delimited string. It simply translates

    WHERE 6 IN (a.allowed_activity) 
    

    to:

    WHERE 6 = a.allowed_activity
    

提交回复
热议问题