SQL Like 与 = 在没有通配符的情况下功能不同

SQL Like vs. = Function Differently Without Wildcards

我有一个查询,其中从 like 更改为 = 结果匹配。我的理解是 =like 的功能相同,除非存在通配符。 _% 都不在我的字符串中。我的字符串中出现的字符是:

acdeijknoprtuy4@-.

我的查询是:

SELECT * FROM UserEmails WHERE email like ?

返回0个结果,我改成:

SELECT * FROM UserEmails WHERE email = ?

我得到了 1 个预期结果。这是 运行 mysql 5.6.41 的 percona 版本。

您的字符串或匹配的数据库值很可能有尾随空格。

请参阅 MySQL 5.6 的文档:

All MySQL collations are of type PAD SPACE. This means that all CHAR, VARCHAR, and TEXT values are compared without regard to any trailing spaces. “Comparison” in this context does not include the LIKE pattern-matching operator, for which trailing spaces are significant.

In MySQL 8.0:

Most MySQL collations have a pad attribute of PAD SPACE. The exceptions are Unicode collations based on UCA 9.0.0 and higher, which have a pad attribute of NO PAD.