SQL 中的“LIKE”和“=”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1504990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between "LIKE" and "=" in SQL?
提问by aviraldg
Is there any difference between:
之间有什么区别:
SELECT * FROM users WHERE username="davyjones"
and
和
SELECT * FROM users WHERE username LIKE "davyjones"
(I think I've bungled up the syntax... pardon me for that,
I'm mostly a desktop app development guy)
(我想我已经搞砸了语法......请原谅我,
我主要是一个桌面应用程序开发人员)
采纳答案by Milan Babu?kov
As per SQL standard, the difference is treatment of trailing whitespace in CHAR columns. Example:
根据 SQL 标准,区别在于处理 CHAR 列中的尾随空格。例子:
create table t1 ( c10 char(10) );
insert into t1 values ('davyjones');
select * from t1 where c10 = 'davyjones';
-- yields 1 row
select * from t1 where c10 like 'davyjones';
-- yields 0 rows
Of course, assuming you run this on a standard-compliant DBMS. BTW, this is one the main differences between CHARs and VARCHARs.
当然,假设您在符合标准的 DBMS 上运行它。顺便说一句,这是 CHAR 和 VARCHAR 之间的主要区别之一。
回答by code_burgar
LIKE
allows partial matching / use of wildcards, while =
checks for exact matches.
LIKE
允许部分匹配/使用通配符,同时=
检查完全匹配。
For example
例如
SELECT * FROM test WHERE field LIKE '%oom';
Will return rows where field value is any of the following:
将返回字段值为以下任何一项的行:
Zoom, Boom, Loom, Groom
回答by Erich
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.
在这种情况下,结果中不会出现差异。但是,它使用不同的方法进行比较,并且“LIKE”会慢得多。
Check out this for examples of LIKE : http://www.techonthenet.com/sql/like.php
看看这个 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 willbe different. See this answerfor more details. When using VARCHAR (presumably the norm), the above are equivalent and equals is to be preferred.
更新:请注意,有一个关键的区别,当涉及到CHAR类型列在其结果会有所不同。有关更多详细信息,请参阅此答案。当使用 VARCHAR(大概是规范)时,以上是等效的,并且等于是首选。
回答by T.J. Crowder
LIKE
allows wildcards like %
(any number of characters here) and _
(one character here).
LIKE
允许使用通配符,如%
(此处为任意数量的字符)和_
(此处为一个字符)。
SELECT * FROM users WHERE username LIKE 'joe%'
Selects all usernames starting with joe
.
选择所有以 开头的用户名joe
。
回答by subhash
create table A (id int,name varchar(30))
insert into A values(4,'subhash')
Use the trailing whitespace to search the name field:
使用尾随空格搜索名称字段:
select * from A where name='Subhash '
--Yields 1 row
select * from A where name like 'Subhash '
--Yields 0 row
回答by Larsenal
LIKE searches for a pattern.
LIKE 搜索模式。
/* Returns all users whose username starts with "d" */
SELECT * FROM users WHERE username LIKE 'd%'
/* Returns all users whose username contains "dav" */
SELECT * FROM users WHERE username LIKE '%dav%'
回答by Richard
That will give you the same result. However, LIKE allows wildcards, for example...
这会给你同样的结果。但是,LIKE 允许通配符,例如...
SELECT * FROM users WHERE username LIKE 'davy%'
The only syntax problem was double quotes instead of single quotes
唯一的语法问题是双引号而不是单引号
回答by auujay
回答by Daniel May
The LIKE
condition allows you to use wildcards:
在LIKE
条件允许您使用通配符:
SELECT * FROM suppliers
WHERE supplier_name like 'Hew%';
and Equals =
is used for equality matching.
和 Equals=
用于相等匹配。
回答by P Sharma
Like is pattern matching operator and =
is exact matching operator. i.e. where name like W%
it means start with W
and after that one or more characters
and =
i.e. where name ='James'
this is exact matching
Like 是模式匹配运算符,=
是精确匹配运算符。ie where name like W%
it 意思是从W
那个一个或多个字符开始和之后,=
ie where name ='James'
this is完全匹配