简体   繁体   English

MySQL 使用等于 (=) 和 LIKE 以及字母数字标准值更新查询

[英]MySQL Update query using equals (=) and LIKE with alphanumeric criteria value

Recently noticed that a working UPDATE query has no longer same results with where clause that contains alphanumeric values leading with number, appreciate any help.最近注意到,一个有效的UPDATE查询不再具有与 where 子句相同的结果,其中包含以数字开头的字母数字值,感谢任何帮助。

UPDATE query: UPDATE查询:

UPDATE `user` SET last_name = 'newTest' where `identifier` = '123abc';

ISSUE : This query updates all records with identifier begins with 123... ISSUE : 此查询更新identifier以 123 开头的所有记录...

Fixed temporary the issue by using LIKE (see below):通过使用LIKE临时修复了该问题(见下文):

UPDATE `user` SET last_name = 'newTest' where `identifier` LIKE '123abc';

But afraid there is some other similar cases in my code that could causes unacceptable data loss for clients.但担心我的代码中还有其他类似的情况可能会导致客户无法接受的数据丢失。

EDIT : query with the issue is when I update another identifier that has the exact number leading other identifiers:编辑:问题的查询是当我更新另一个标识符时,该标识符具有领先于其他标识符的确切数字:

UPDATE `user` SET last_name = 'newTest' where `identifier` = 123;

Tech.技术。 versions used: php 7, mysql 5.6, RedBean 5, OS Alpine 3.11使用的版本:php 7、mysql 5.6、RedBean 5、OS Alpine 3.11

The problem is that you compare a string value and a numeric value:问题是你比较一个字符串值和一个数值:

where `identifier` = 123

so Mysql does an implicit conversion of the column identifier to numeric and since the values 123abc or 123... start with 123 , they are all converted to 123 and the condition returns TRUE for all the values that start with 123 .因此 Mysql 将列identifier隐式转换为数字,并且由于值123abc123...123开头,它们都被转换为123并且对于所有以123开头的值,条件返回TRUE
You can see this behavior here .您可以在此处查看此行为。
What you want is string comparison.你想要的是字符串比较。
So change to:所以改为:

where `identifier` = '123...'

This way you do a string comparison.这样你就可以进行字符串比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM