在数据库中新建一张测试表t_user,包含三个字段'id','name','grilfriend',字段很容易看出,这是记录一个人的女朋友的表,注意这里的‘firlfriend’字段可以是多个人名,之间用逗号分开,如‘杨幂01,杨幂02’;这样的数据存储我们也经常见到。

CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`girlfriend` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

插入几条测试数据

INSERT INTO `t_user` VALUES ('', '狗蛋', '林心如8,安以轩8,范冰冰8');
INSERT INTO `t_user` VALUES ('', '狗剩', '林心如88,杨幂8,杨颖8');
INSERT INTO `t_user` VALUES ('', '石头', '范冰冰88,杨颖88');

数据显示如下:

下面我想查询'grilfrind中'包含”杨颖8″是谁的女朋友:

like 查询:

SELECT * from t_user where girlfriend like '%杨颖8%'

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQ0AAABLCAIAAADlBkWaAAAK4ElEQVR4nO1dy47cxhWt79EykIlRfYL1EUE4moL/IovAsXta0PAT7KzkTdQtIDTsUVZZJcjGmmkvwtoZSRCMHLQ1048ZdTe9qAfrxSK7myyS9j24EJpksetU8Z5bVZyuK5QDAIAqoK4JAAADAOgEAKgG18kXAACgHIVOKiVFKf1hlXdllNIWw0XLGDR5AKUUdBICgyYPAJ0EwqDJA0AngTBo8gDQSSAMmjzgUJ1cX/wGPfn9teXNE4Kii7+BTixUk88unqAnF5l9ISWIpEohhIrjqntrIyVHfsEvG6CTQGhEJ9nFE0si/ntrA3TiRdPzLtBJCY4gX+ikRWcGnXih6WS1fvDYZrOllP71x9xnXxH0+OJP/jKHGqXUz7C3xrru0Ntfx+jZZP2wWj9MTlE0/r4VktNnKHp+3XVH9dPY4ztIJ/+8eITIZ9yD06dIQQidvI7Ryfn4GauwcJ3Z80iwiKdKySk/H08fVlPrrvXrWNzVkhd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uQmCC" alt="" />

FIND_IN_SET查询:

SELECT * from t_user where FIND_IN_SET('杨颖8',girlfriend)

下面我想查询'grilfrind中'包含”杨颖88″是谁的女朋友:

like 查询:

SELECT * from t_user where girlfriend like '%杨颖88%'

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARAAAAAyCAIAAAAbexPLAAAHjUlEQVR4nO1cy3LbNhS935Nlx+HY/ITkIzqlbEz+ootOm8jy2PwEt6tkU0ueKTup3VVX7XSTyMqixC7TdjpOO24SPexIShd4EARAiKLFl3vP3AUJXQiHFzjABS0DPiIQiNyAYwQCkRtwfHy8UlWU0tfTj3UZpbSCmaMktJo8wuw+FEy5aDV5BAqmarSaPAIFUzVaTR6BgqkarSaPuLVgLo4+gZ3PL4xh3SfgHf2EgjGwmnx8tAM7R7H5QUSARIoTQHK/qm5uROSWX3C3gYKpGhsRTHy0Y2jFXTc3UDBOlJaSoWAycAvyiWBKHNUoGCfsgpnObhw2ny8opT/+/dFlzwhsHX3j9ilqlFI3w8YaC13R6qcB7PZnN9PZTb8DXu9VKSQHu+AdXNQdqGYa677bCebXo3tAHvOhHD0EBVUI5jSA7f3eLmswGUOjA0+wCAaK54CXB4Ob6cCoNTsNRK2ShqMhGNni9n5PjNTRgceF8Wrfg2BwGgCAd3AhBNPvyBDv9jWfpK71cTLCpXgCb6v+0dlAyxTMZHrtsAzBRA8BHj7jY/rxp+UKRpAZBAAQDCbT68lFz4Pt7sX1ZDrqBr0hc+h3ADp96en1hrxQrSUdWPXryXTU9SDou4JQzFjoVPKilVHXE/QSSqOuB4I/8+fX/QC8/dHE9FnxONZwqTSu+4GggZbRfQUFc/5mef5mef7L4T3Y++rN8vzpHmwdfs0KzduNWnrMqcNCXl8P97fFhGkOINu1Mm/zeZaPyM1HnN/2O6mhKW/TglF0mymYxEfWtT+OLVxZNNAyum9twSwWzRbMRc+Tw90+42YIpvyBwkLHb8sWjOVxUDAb6D6LYMaTmcPmiwWl9OxyeXa5PPv58B7sfXm5PLv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HTWhqUY+hqIxiRPTFQiEdESCRdb8vBGPM3bmmyvKgRjyRSVow2kRNxIPw0eiHkbZKrvZR5JnaSJiFKreQq4OEMQ+yVIqVrlUwazfdZKwhGO3clgpJ6kmZMkn7YZzxgixrhTFy/4ph/zuMlphZ9KJllNr6tMonHUJ+Zy2UtyHzkFlVKtQsusaWhVgEs37TTYZdMPK4CRkK8xiKcnlpy7l7059fMDwrtydx1UAjnwwu81Us+4Ckd+/SM3nPlMMnFURRai1MgUdWDOhkYKfejiXrtNSRIpiCTTcUuQRjPbelbGaO1/RFNv3865Kviojtq8uHlpJpM3gCkd0QtiWx7XZ49Tw+xvushIBWmFqBIwIk5AXqsBbzGXOOiB9G6p9htODnbboNsAtGns8CAFnnttRCl2GtFYZ3THZ+VnEykPqZgq5fXe/aVsEyERjbCavPGtBZJS+6xMqgLyipnYuQlbnzvBvIEEyOc1tqocvQ6p9jNZx8kXeI6tyU2gY5f3zRTtgFk+fcllroMjR8zLnRavKIDMHkOLelFroMrR5zrSaPsAsmzzEUtdBlaPWYazV5BAqmarSaPMIumDxnZ1whEP9LWARTlVwRiNYDBYNArAEUDAKxBlAwCMQa+A+g4iWFy3NnEQAAAABJRU5ErkJggg==" alt="" />

FIND_IN_SET查询:

SELECT * from t_user where FIND_IN_SET('杨颖88',girlfriend)

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

附加:函数简介

FIND_IN_SET(str,strlist)

假如字符串str 在由N 子链组成的字符串列表strlist 中, 则返回值的范围在 1 到N 之间 。一个字符串列表就是一个由一些被‘,’符号分开的自链组成的字符串。如果第一个参数是一个常数字符串,而第二个是type SET列,则 FIND_IN_SET() 函数被优化,使用比特计算。如果str不在strliststrlist 为空字符串,则返回值为 0 。如任意一个参数为NULL,则返回值为 NULL。 这个函数在第一个参数包含一个逗号(‘,’)时将无法正常运行。

mysql中IFIND_IN_SET和like的区别的更多相关文章

  1. 用count(*)还是count(列名) || Mysql中的count()与sum()区别

    Mysql中的count()与sum()区别   首先创建个表说明问题 CREATE TABLE `result` (   `name` varchar(20) default NULL,   `su ...

  2. (转)MySQL中In与Exists的区别

    背景:总结mysql相关的知识点. 如果A表有n条记录,那么exists查询就是将这n条记录逐条取出,然后判断n遍exists条件. select * from user where exists s ...

  3. MySQL中interactive_timeout和wait_timeout的区别【转】

    在用mysql客户端对数据库进行操作时,打开终端窗口,如果一段时间没有操作,再次操作时,常常会报如下错误: ERROR 2013 (HY000): Lost connection to MySQL s ...

  4. Mysql中函数和存储过程的区别

    Mysql中函数和存储过程的区别 存储过程: 1.       可以写sql语句 2.       inout,out构造返回值 3.       调用:call:存储过程名称 4.       可以 ...

  5. mysql中in和exist的区别

    mysql中in和exists的区别 -- in写法select * from A where A.id in (select bid from  B ) and A.name in (select ...

  6. MySQL中 utf8与utf8mb4的区别

    MySQL中 utf8与utf8mb4的区别 一.简介 ​ MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode.好在 ...

  7. MySQL 中NULL和空值的区别

    平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...

  8. mysql中char与varchar的区别分析(补充一句,int和integer没区别)

    转自:http://www.jb51.net/article/23575.htm 在mysql教程中char与varchar的区别呢,都是用来存储字符串的,只是他们的保存方式不一样罢了,char有固定 ...

  9. MySQL 中NULL和空值的区别 (转载 http://blog.sina.com.cn/s/blog_3f2a82610102v4dn.html)

    平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...

随机推荐

  1. Notes on <Assembly Language step by step>

    By brant-ruan Yeah, I feel very happy When you want to give up, think why you have held on so long. ...

  2. APDU

    # APDU # 定义:APDU(ApplicationProtocolDataUnit--应用协议数据单元).协议数据单元PDU(ProtocolDataUnit)是指对等层次之间传递的数据单位.协 ...

  3. HQL基础查询语句

    HQL基础查询语句 1.使用hql语句检索出Student表中的所有列 //核心代码 @Test public void oneTest() { Query query=session.createQ ...

  4. document对象补充

    五.相关元素操作: var a = document.getElementById("id");                找到a: var b = a.nextSibling ...

  5. java内存设置

    在上边红色框内加入java内存设置命令: -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m

  6. Javascript备忘复习笔记2

    一.函数与形参 1.函数 function abs(x) { if (x >= 0) { return x; } else { return -x; } } alert(abs(-10)); 2 ...

  7. Java读写文本文件

    1 字符输入(FileReader , char) import java.io.IOException; import java.io.FileReader; public class ep10_1 ...

  8. win7 远程桌面关机

    在任务管理器中, 打开运行窗口, 执行 shutdown -s 命令, 将在30秒后关闭win7, 如果需要更快, 加上 -t 10 参数 关于 shutdown 的命令行说明: C:\Users\R ...

  9. QT QToolBox类

    QToolBox类的创建 //drawer.h #ifndef DRAWER_H #define DRAWER_H #include <QToolBox> #include <QTo ...

  10. 基于Spring的简易SSO设计

    通常稍微规模大一些的企业,内部已经有很多的应用系统,多个系统整合首先要解决的便是“统一登录(SSO)”问题,之前写过一篇 利用Membership实现SSO(单点登录) ,java环境下已经有一些开源 ...