difference between where_in and find_in_set

后端 未结 3 1707
暗喜
暗喜 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:09

    The following code is incorrect:

    $this->db->where("FIND_IN_SET($activity_id, a.allowed_activity) !=",0);
    

    I'm surprised this even runs without error, but one possibility is that it is generating a WHERE clause which compares a string against 0. Of course, they will never be equal, and so this will always be true.

    If you want to call the native FIND_IN_SET() function, then you would need to use whereRaw():

    $this->db->whereRaw("FIND_IN_SET($activity_id, a.allowed_activity) !=0");
    

    Using WHERE IN (...) is incorrect here, because you have all the values in a single row, separated from commas. WHERE IN would make sense if you wanted to filter for certain values from many rows, each column having a single value.

提交回复
热议问题