且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Mysql Like +通配符vs等于运算符

更新时间:2022-04-02 23:45:20

此语句返回my_field = '13a'的行:

SELECT * FROM my_table WHERE my_field=13

因为MySQL在比较期间执行了从字符串到数字的类型转换,将'13a'转换为13. 此文档页面中的更多内容.

Because MySQL performs type conversion from string to number during the comparison, turning '13a' to 13. More on that in this documentation page.

添加引号会将整数转换为字符串,因此MySQL仅执行字符串比较.显然,'13'不能等于'13a'.

Adding quotes turns the integer to a string, so MySQL only performs string comparison. Obviously, '13' cannot be equal to '13a'.

LIKE子句始终执行字符串比较(除非其中一个操作数为NULL,在这种情况下结果为NULL).

The LIKE clause always performs string comparison (unless either one of the operands is NULL, in which case the result is NULL).