SQL语句中find_in_set、like、in的区别

find_in_set(str,strlist)函数

    str  要查询的字符串

      strlist  字段名, 参数以“,”分割  如(1,2,3,4)

     查询字段(strlist)中包含(str)的结果,返回结果为null,或者记录。

基本语法使用

   1、in查询相当于多个or条件的叠加。例如:

         select  *  from  user  where  user_id  in  (1,2,3);

        等效于

         select  *  from  user  where  user_id = 1 or user_id = 2 or user_id = 3

        not  in 与 in相反

     2、+---+--------+

          | id | follow_id |

          +----+-------+

          |   1 | 14,15 |

           | 2  |  13       |

         这时,select  * from  test where  find_in_set('5',follow_id);这样是查不到的,返回值为null,因为follow_id中没有“5”这个值,它不同于like模糊查询,它是以“,”来分割。

         如果使用like查询,查询结果为id=1的一条记录。

sql中like用法,可以参考 :http://blog.sina.com.cn/s/blog_a74f39a201018jal.html

总结:

find_in_set与like的区别

     like是广泛的模糊匹配,字符串中没有分隔符,find_in_set是精确匹配,字段值以英文“,”分隔,find_in_set查询的结果要小于like查询的结果。

find_in_set与in的区别

     select * from table_name  where  'test' in (list);

        select * from table_name  where  find_in_set('test',list);

        所以如果list是常量,则直接用in;如果list是变量,则要使用find_in_set()函数

猜你喜欢

转载自blog.csdn.net/xueshao110/article/details/80606960
今日推荐