没有“%”的mysql“like”与“=”条件的工作方式不同

mctunoxg  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(335)

这个问题在这里已经有答案了

相等(=)与相似(15个答案)
为什么相等运算符和相似运算符之间的相等比较有区别(3个答案)
where子句中无百分号的sql like和equal(=)之间的差异(2个答案)
两年前关门了。
我有下面的mysql查询。

select * from node where title LIKE 'born in care shelter breed';

返回空集。但是当我尝试下面的查询时

select * from node where title = 'born in care shelter breed';

它返回1个结果。
两者有什么区别?我无法避免like操作符,因为查询是在一些条件检查之后创建的

kqhtkvqz

kqhtkvqz1#

我猜你的字典里有尾随空格 title 现场。使用 = 符号不考虑尾随空格。注意以下几点:

CREATE TABLE node
(title VARCHAR(99));

INSERT INTO node (title)
VALUES ('born in care shelter breed '); -- Note the space at the end

SELECT * FROM node WHERE title LIKE 'born in care shelter breed';
SELECT * FROM node WHERE title = 'born in care shelter breed';

请注意,第一个select语句如何返回零结果,而第二个语句如何找到行。
sql小提琴
文档在字符串比较页上讨论了这一点,说明:
特别是,尾随空格是重要的,这对于使用=运算符执行的char或varchar比较是不正确的

7xllpg7q

7xllpg7q2#

在where子句中使用like运算符来搜索列中的指定模式。
有两个通配符与like运算符一起使用:

% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character

我想下面会再排

select * from node where title LIKE '%born in care shelter breed%';

了解更多

相关问题