SELECT * FROM pet WHERE name LIKE 'Spot';

SELECT * FROM pet WHERE name = 'Spot';

上面这两个sql语句有什么区别呢? 今天在值班时遇到一个人名联想的问题,当用户输入xyz,只出现一个人名,而如果xyza, 则出现了好几个候选人名,好奇了下搜索结果,查了下代码,代码先用的全匹配,再用了模糊匹配,如果输入的内容在人名表里存在,则精确出现该人名,如果内容不能全匹配,则走到模糊匹配上,全匹配的时候用的是like 不带百分号,一时好奇,不带百分号的like 和等号的区别,于是有了这篇文章,实际上,没有通配符的LIKE在功能上等同于=。但是,他们又有点不一样! 这就要说到他们的工作方式,在网上搜了一圈,发现一篇比较好的解释:

LIKE and = are different operators. Most answers here focus on the wildcard support, which is not the only difference between these operators!

= is a comparison operator that operates on numbers and strings. When comparing strings, the comparison operator compares whole strings.

LIKE is a string operator that compares character by character.

To complicate matters, both operators use a collation which can have important effects on the result of the comparison.

How Does = Work?

The SQL Standard § 8.2 describes how = compares strings:

The comparison of two character strings is determined as follows:

a) If the length in characters of X is not equal to the length in characters of Y, then the shorter string is effectively replaced, for the purposes of comparison, with a copy of itself that has been extended to the length of the longer string by concatenation on the right of one or more pad characters, where the pad character is chosen based on CS. If CS has the NO PAD attribute, then the pad character is an implementation-dependent character different from any character in the character set of X and Y that collates less than any string under CS. Otherwise, the pad character is a .

b) The result of the comparison of X and Y is given by the collating sequence CS.

c) Depending on the collating sequence, two strings may compare as equal even if they are of different lengths or contain different sequences of characters. When the operations MAX, MIN, DISTINCT, references to a grouping column, and the UNION, EXCEPT, and INTERSECT operators refer to character strings, the specific value selected by these operations from a set of such equal values is implementation-dependent.

(Emphasis added.)

What does this mean? It means that when comparing strings, the = operator is just a thin wrapper around the current collation. A collation is a library that has various rules for comparing strings. Here’s an example of a binary collation from MySQL:

static int my_strnncoll_binary(const CHARSET_INFO *cs attribute((unused)),

const uchar *s, size_t slen,

const uchar *t, size_t tlen,

my_bool t_is_prefix)

{

size_t len= MY_MIN(slen,tlen);

int cmp= memcmp(s,t,len);

return cmp ? cmp : (int)((t_is_prefix ? len : slen) - tlen);

}

This particular collation happens to compare byte-by-byte (which is why it’s called “binary” — it doesn’t give any special meaning to strings). Other collations may provide more advanced comparisons.

For example, here is a UTF-8 collation that supports case-insensitive comparisons. The code is too long to paste here, but go to that link and read the body of my_strnncollsp_utf8mb4(). This collation can process multiple bytes at a time and it can apply various transforms (such as case insensitive comparison). The = operator is completely abstracted from the vagaries of the collation.

How Does LIKE Work?

The SQL Standard § 8.5 describes how LIKE compares strings:

M LIKE P

is true if there exists a partitioning of M into substrings such that:

i) A substring of M is a sequence of 0 or more contiguous s of M and each of M is part of exactly one substring.

ii) If the i-th substring specifier of P is an arbitrary character specifier, the i-th substring of M is any single .

iii) If the i-th substring specifier of P is an arbitrary string specifier, then the i-th substring of M is any sequence of 0 or more s.

iv) If the i-th substring specifier of P is neither an arbitrary character specifier nor an arbitrary string specifier, then the i-th substring of M is equal to that substring specifier according to the collating sequence of the , without the appending of characters to M, and has the same length as that substring specifier.

v) The number of substrings of M is equal to the number of substring specifiers of P.

(Emphasis added.)

This is pretty wordy, so let’s break it down. Items ii and iii refer to the wildcards _ and %, respectively. If P does not contain any wildcards, then only item iv applies. This is the case of interest posed by the OP.

In this case, it compares each “substring” (individual characters) in M against each substring in P using the current collation.

结论:

The bottom line is that when comparing strings, = compares the entire string while LIKE compares one character at a time. Both comparisons use the current collation. This difference leads to different results in some cases, as evidenced in the first example in this post.

mysql contain和like_mysql不带%的like 与等号之间的区别相关推荐

  1. php计算属性集的闭包,关于swift:计算属性与带闭包的属性集之间的区别

    我是Swift的新手. 计算属性和设置为闭包的属性之间有什么区别? 我知道每次都会重新计算一个计算属性. 封盖是否有所不同? 即 关闭: var pushBehavior: UIPushBehavio ...

  2. mysql socket tcp udp_TCP、UDP、HTTP、SOCKET之间的区别

    IP:网络层协议: TCP和UDP:传输层协议: HTTP:应用层协议: SOCKET:TCP/IP网络的API. TCP/IP代表传输控制协议/网际协议,指的是一系列协议. TCP和UDP使用IP协 ...

  3. mysql csv 表头_Mysql实例mysql 导出CSV文件 并带表头的方法

    <Mysql实例mysql 导出CSV文件 并带表头的方法>要点: 本文介绍了Mysql实例mysql 导出CSV文件 并带表头的方法,希望对您有用.如果有疑问,可以联系我们. 参考官方文 ...

  4. 金九银十!“68道 Redis+168道 MySQL”精品面试题(带解析),你背废了吗?

    前言 谈起 Redis 和 MySQL,皆是广大 程 序 猿(媛)朋友面试跳槽必踩的两个坑.那么,关于Redis与MySQL,面试官最爱问哪些问题呢?不知道也不用慌,我已整理了这"68道 R ...

  5. “68道 Redis+168道 MySQL”精品面试题(带解析),你背废了吗?

    谈起 Redis 和 MySQL,皆是广大 程 序 猿(媛)朋友面试跳槽必踩的两个坑.那么,关于Redis与MySQL,面试官最爱问哪些问题呢?不知道也不用慌,我已整理了这"68道 Redi ...

  6. “68 道 Redis+168 道 MySQL”精品面试题(带解析),你背废了吗?

    谈起 Redis 和 MySQL,皆是广大 程 序 猿(媛)朋友面试跳槽必踩的两个坑.那么,关于 Redis 与 MySQL,面试官最爱问哪些问题呢?不知道也不用慌,我已整理了这"68 道 ...

  7. oracle类型sql转为mysql_Oracle和MySql之间SQL区别(等效转换以及需要注意的问题)...

    >本篇博文是Oracle和MySQL之间的等效SQL转换和不同,目前市面上没有转换两种SQL的工具,小编觉得以后也不一定会有,于是在业余时间整理了一下,如果有什么错误之处请留言告知,小编也是刚 ...

  8. mysql索引之间的区别

    2019独角兽企业重金招聘Python工程师标准>>> mysql索引类型: ①主键索引:与唯一索引之间的区别就在于不允许有空值,创建主键时会自动创建此索引. ②普通索引:最基本的索 ...

  9. mysql 迭代更新_MySQL、MongoDB、Redis 数据库之间的区别与使用(本章迭代更新)

    MySQL.MongoDB.Redis 数据库之间的区别与使用 MySQL.MongoDB.Redis 数据库之间的区别与使用(本章迭代更新) update:2019年2月20日 15:21:19(本 ...

  10. php里面sql是什么意思,MySQL和SQL是什么?MySQL和SQL之间的区别有哪些

    MySQL和SQL之间的区别有哪些?很多PHP的初学者,对MySQL,MyAdmin和SQL有什么区别并不是很清楚?下面 第一PHP社区 就带领大家来学习一下MySQL和SQL之间的区别.[推荐阅读: ...

最新文章

  1. FoundationDB Record Layer 宣布开源,提供关系数据库功能
  2. Linux下的USB总线驱动 1
  3. 计算机导论第一章试题及答案,计算机导论第一章试题
  4. matlab绘制球面模型_MATLAB采用surf/surfc/surfl/surfnorm绘制球体
  5. 科大讯飞2020完整事件抽取系统(bert+数据集)
  6. 【Elasticsearch】10分钟查询一个petabyte的云存储容量
  7. OpenCV C++ 常用功能
  8. ENVI5.3.1使用Landsat 8影像进行主成分分析实例操作
  9. Flexsim在固定资源类中没有分拣传送带?
  10. 126套Unity3D视频教程全集(包含入门、编程、特效、UI、动画、实战等等)
  11. 【摄像头】Global Shutter(全局快门)与Rolling Shutter(卷帘快门)的区别与比较...
  12. 简单理解串行计算、并行计算、分布式计算、网格计算与云计算
  13. #define宏定义(每天一个小虾米)
  14. 升级Monterey的血泪史~~哭唧唧~~
  15. exlc表格怎么换行_excel怎么换行 excel表格内如何换行
  16. MoviePy - 中文文档4-MoviePy实战案例-重新构建15世纪舞蹈视频
  17. 十大经典三维动画制作软件
  18. go 图片转base64
  19. 基于Cortex-M4的塔机安全监控/防碰撞系统
  20. 电机不动 米兔机器人_米兔机器人滞后开箱+使用感受

热门文章

  1. 20190916每日一句
  2. 20190904每日一句
  3. 传智播客Java引用和数值类型思考
  4. Atitit git push 报错 remote: error: hook declined to update git push 报错 remote: error: hook declined
  5. atitit.提升备份文件复制速度(4) ---数据挖掘 获取回收站文件列表
  6. 拨号720错误解决记.txt
  7. 苏槐: 数据治理的本质及实践
  8. (转)用纸笔解释比特币挖矿算法原理
  9. (转)ICO是区块链与生俱来的特性,是金融深化的终局
  10. 性能优化:缓存使用的秘密