“LIKE”和“LIKE”有什么区别?和“=”在 SQL 中?
有什么区别:
SELECT * FROM users WHERE username="davyjones"
和
SELECT * FROM users WHERE username LIKE "davyjones"
Is there any difference between:
SELECT * FROM users WHERE username="davyjones"
and
SELECT * FROM users WHERE username LIKE "davyjones"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
LIKE
允许部分匹配/使用通配符,而=
检查精确匹配。例如,
将返回字段值为以下任意一项的行:
LIKE
allows partial matching / use of wildcards, while=
checks for exact matches.For example
Will return rows where field value is any of the following:
根据 SQL 标准,区别在于 CHAR 列中尾随空格的处理。示例:
当然,假设您在符合标准的 DBMS 上运行它。顺便说一句,这是 CHAR 和 VARCHAR 之间的主要区别之一。
As per SQL standard, the difference is treatment of trailing whitespace in CHAR columns. Example:
Of course, assuming you run this on a standard-compliant DBMS. BTW, this is one the main differences between CHARs and VARCHARs.
在这种情况下,结果不会出现任何差异。然而,它使用不同的比较方法,并且“LIKE”会慢得多。
查看 LIKE 的示例:http://www.techonthenet.com/sql/like。 php
在这种情况下,您仍然想使用 equals。
更新:请注意,对于 CHAR 类型列,存在一个关键的区别,其中结果将不同。有关更多详细信息,请参阅此答案。当使用VARCHAR(大概是规范)时,以上是等价的,并且优先使用equals。
In that case, there is no difference that would come up in the results. However, it uses a different method for comparision, and the "LIKE" would be much slower.
Check out this for examples of LIKE : http://www.techonthenet.com/sql/like.php
In this case, you still want to use the equals.
Update: Note that there is a crucial difference when it comes to CHAR type columns in which the results will be different. See this answer for more details. When using VARCHAR (presumably the norm), the above are equivalent and equals is to be preferred.
使用尾随空格搜索名称字段:
Use the trailing whitespace to search the name field:
LIKE
允许使用通配符,如%
(此处为任意数量的字符)和_
(此处为一个字符)。选择以
joe
开头的所有用户名。LIKE
allows wildcards like%
(any number of characters here) and_
(one character here).Selects all usernames starting with
joe
.LIKE 搜索模式。
LIKE searches for a pattern.
这会给你同样的结果。但是,LIKE 允许使用通配符,例如...
唯一的语法问题是双引号而不是单引号
That will give you the same result. However, LIKE allows wildcards, for example...
The only syntax problem was double quotes instead of single quotes
LIKE 支持通配符。通常它使用 % 或 _ 字符作为通配符。
使用不带通配符的 LIKE 运算符与使用 = 运算符相同。
LIKE supports wildcards. Usually it uses the % or _ character for the wildcard.
Using the LIKE operator with no wildcards is the same as using the = operator.
LIKE
条件允许您使用通配符:查看更多示例。
和 Equals
=
用于相等匹配。The
LIKE
condition allows you to use wildcards:See more examples.
and Equals
=
is used for equality matching.Like 是模式匹配运算符,
=
是精确匹配运算符。即名称如W%
表示以W
开头,后面是一个或多个字符和
=
即其中 name='James'
这是完全匹配的Like is pattern matching operator and
=
is exact matching operator. i.e. where name likeW%
it means start withW
and after that one or more charactersand
=
i.e. where name='James'
this is exact matching据我所知,您编写的两个选择没有任何区别,只是时间成本不同。通常将
LIKE
与%
一起使用,意思是“任何字符串”。我认为还有一个字符可以与LIKE
一起使用来表示“任何字符”,不通过谷歌搜索就不确定它是什么。但是随着您的两个选择的进行,我看到的唯一区别是不同的运行时间,因为
LIKE
用于 regexp-sort-of-fashion 中。As far as I know, there is no difference but a time cost to the two selects you wrote. Usually one uses
LIKE
together with%
, meaning 'any string'. I think there's also a character that can be used withLIKE
for 'any character', not sure what that is without googling.But as your two selects go, the only difference I see is a different run time, since
LIKE
is used in a regexp-sort-of-fashion.Equals
'='
只是为了相等。另一方面,LIKE
支持 SQL 通配符匹配。因此,使用
LIKE
,您可以执行name like '%jones'
来获取所有以jones结尾的名称。对于LIKE
,百分号'%'
字符可以是任何长度,长度为零或更大,下划线字符'_'
可以是任何一个特点。Equals
'='
is just for equality. On the other hand,LIKE
supports SQL wildcard matching.So, with
LIKE
you can doname like '%jones'
to get all the names ending in jones. WithLIKE
, the percent'%'
character is anything, length zero or more, and the underscore character,'_'
, is any one character.Like
让您可以使用通配符运算符,您可以在like 'davyjon%'
的情况下使用它来获取以davyjon
开头的所有结果code>,要获得准确的结果,您可以放置'davyjones'
并且在这种情况下您也可以使用=
Like
gets you to work with wild card operators, you may use it in your case forlike 'davyjon%'
to get all the results starting withdavyjon
, and to get the exact you may place'davyjones'
and you may also use=
in this case我知道这个问题太老了,但即使在今天,我得到很多其他人也会尝试找出答案。在许多书中,在大多数情况下,LIKE 可能比“-”慢,除非您想比较字符串。
I know this question is too old, but I get many others will try to find out the answer even nowadays. In many books, LIKE may come slower than '-' in most case, except when you want to compare String.