gpt4 book ai didi

mysql - LIKE 和 = 在 MySQL 中以同样的方式处理空格吗?

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:29 25 4
gpt4 key购买 nike

当运行 MySQL 查询时,我有一个奇怪的现象,该查询有一些条件 LIKE=

SELECT
...
FROM
...
WHERE
products.productcode LIKE '590082QRV040'
AND ...

出于某种原因,在数据库中,某些产品代码在末尾存储了一个空格字符(大概是在插入数据的某处缺少修整)。因此,例如上面的产品代码实际上存储为 '590082QRV040 '。奇怪的是,如果我使用以下条件运行查询:products.productcode LIKE '590082QRV040' 没有匹配项,但是对于 products.productcode = '590082QRV040' 我得到了想要的结果。我只是想知道是什么原因。我认为 LIKE= 的区别在于 LIKE 中允许使用通配符。但现在我什至没有通配符,所以两者的行为应该完全相同。

最佳答案

documentation在这一点上很清楚:

Per the SQL standard, LIKE performs matching on a per-character basis, thus it can produce results different from the = comparison operator:

. . .

In particular, trailing spaces are significant, which is not true for CHAR or VARCHAR comparisons performed with the = operator:

我强烈建议您修复数据:

update products p
set p.productcode = trim(p.productcode);

关于mysql - LIKE 和 = 在 MySQL 中以同样的方式处理空格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35940412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com