zoukankan      html  css  js  c++  java
  • SQL 语句中的in、find_in_set、like的区别

    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相反,如下
    select * from user where user_id not in (1,2,3);
    等效于
    select * from user where user_id != 1 and user_id != 2 and user_id != 3;

    1.find_in_set基本语法

    FIND_IN_SET(str,strlist)

    str 要查询的字符串,strlist 字段名 参数以”,”分隔 如 (1,2,6,8)
    如果str不在strlist 或strlist 为空字符串,则返回值为 0 。如任意一个参数为NULL,则返回值为 NULL。这个函数在第一个参数包含一个逗号(‘,’)时将无法正常运行。

    +----+---------+-----------+-------------+
    | id | user_id | follow_id | follow_time |
    +----+---------+-----------+-------------+
    | 13 | 15      | 16,15     |  1478096138 |
    | 14 | 15      | 17        |  1478177725 |
    | 15 | 15      | 19        |  1478181035 |
    +----+---------+-----------+-------------+

    比如这张表,SELECT * from test where FIND_IN_SET('5',follow_id);这样是查不到的,返回值为null,因为follow_id中没有”5”这个值,它不同于 like 模糊查询,它是以“,”来分隔值

    like是广泛的模糊匹配,字符串中没有分隔符,Find_IN_SET 是精确匹配,字段值以英文”,
    ”分

  • 相关阅读:
    STL读书笔记
    时间复杂度
    GDB十分钟教程
    lua函数随记
    svn提交时强制添加注释
    按位与、或、异或等运算方法
    mongodb常用语句
    STL容器的基本特性和特征
    C++:模板
    Vector 特性
  • 原文地址:https://www.cnblogs.com/xiaohu1218/p/8531645.html
Copyright © 2011-2022 走看看