“LIKE”和“LIKE”有什么区别?和“=”在 SQL 中?

发布于 2024-08-07 06:36:50 字数 171 浏览 3 评论 0原文

有什么区别:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

沉溺在你眼里的海 2024-08-14 06:36:50

LIKE 允许部分匹配/使用通配符,而 = 检查精确匹配。

例如,

SELECT * FROM test WHERE field LIKE '%oom';

将返回字段值为以下任意一项的行:

Zoom, Boom, Loom, Groom

LIKE allows partial matching / use of wildcards, while = checks for exact matches.

For example

SELECT * FROM test WHERE field LIKE '%oom';

Will return rows where field value is any of the following:

Zoom, Boom, Loom, Groom
节枝 2024-08-14 06:36:50

根据 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

当然,假设您在符合标准的 DBMS 上运行它。顺便说一句,这是 CHAR 和 VARCHAR 之间的主要区别之一。

As per SQL standard, the difference is treatment of trailing whitespace in CHAR columns. Example:

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.

等你爱我 2024-08-14 06:36:50

在这种情况下,结果不会出现任何差异。然而,它使用不同的比较方法,并且“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.

向地狱狂奔 2024-08-14 06:36:50
create table A (id int,name varchar(30))

insert into A values(4,'subhash')

使用尾随空格搜索名称字段:

select * from A where name='Subhash '
--Yields 1 row
select * from A where name like 'Subhash '
--Yields 0 row
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
我是男神闪亮亮 2024-08-14 06:36:50

LIKE 允许使用通配符,如 %(此处为任意数量的字符)和 _(此处为一个字符)。

SELECT * FROM users WHERE username LIKE 'joe%'

选择以 joe 开头的所有用户名。

LIKE allows wildcards like % (any number of characters here) and _ (one character here).

SELECT * FROM users WHERE username LIKE 'joe%'

Selects all usernames starting with joe.

甜妞爱困 2024-08-14 06:36:50

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%'

LIKE searches for a pattern.

/* 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%'
玩套路吗 2024-08-14 06:36:50

这会给你同样的结果。但是,LIKE 允许使用通配符,例如...

SELECT * FROM users WHERE username LIKE 'davy%'

唯一的语法问题是双引号而不是单引号

That will give you the same result. However, LIKE allows wildcards, for example...

SELECT * FROM users WHERE username LIKE 'davy%'

The only syntax problem was double quotes instead of single quotes

蘸点软妹酱 2024-08-14 06:36:50

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.

骑趴 2024-08-14 06:36:50

LIKE 条件允许您使用通配符:

SELECT * FROM suppliers
WHERE supplier_name like 'Hew%';

查看更多示例。

和 Equals = 用于相等匹配。

The LIKE condition allows you to use wildcards:

SELECT * FROM suppliers
WHERE supplier_name like 'Hew%';

See more examples.

and Equals = is used for equality matching.

无言温柔 2024-08-14 06:36:50

Like 是模式匹配运算符,= 是精确匹配运算符。即名称如 W% 表示以 W 开头,后面是一个或多个字符
= 即其中 name ='James' 这是完全匹配的

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

挽心 2024-08-14 06:36:50

据我所知,您编写的两个选择没有任何区别,只是时间成本不同。通常将 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 with LIKE 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.

沫离伤花 2024-08-14 06:36:50

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 do name like '%jones' to get all the names ending in jones. With LIKE, the percent '%' character is anything, length zero or more, and the underscore character, '_', is any one character.

不必在意 2024-08-14 06:36:50

Like 让您可以使用通配符运算符,您可以在 like 'davyjon%' 的情况下使用它来获取以 davyjon 开头的所有结果code>,要获得准确的结果,您可以放置​​ 'davyjones' 并且在这种情况下您也可以使用 =

Like gets you to work with wild card operators, you may use it in your case for like 'davyjon%' to get all the results starting with davyjon, and to get the exact you may place 'davyjones' and you may also use = in this case

小帐篷 2024-08-14 06:36:50

我知道这个问题太老了,但即使在今天,我得到很多其他人也会尝试找出答案。在许多书中,在大多数情况下,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文