Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 查询返回";例如;尽管没有通配符,结果如何?_Mysql_Sql_Equality - Fatal编程技术网

Mysql 查询返回";例如;尽管没有通配符,结果如何?

Mysql 查询返回";例如;尽管没有通配符,结果如何?,mysql,sql,equality,Mysql,Sql,Equality,我真的很困惑。正在运行以下查询: SELECT * FROM `articles` WHERE `form` = 'Depotplåster' AND `size` = 5 返回也以“5”开头的行,尽管我既没有使用类似于的运算符,也没有使用%通配符运算符。为什么 size字段的类型为VARCHAR SELECT * FROM `articles` WHERE `form` = 'Depotplåster' AND `size` = '5' -- this will compare the st

我真的很困惑。正在运行以下查询:

SELECT * FROM `articles` WHERE `form` = 'Depotplåster' AND `size` = 5
返回也以“5”开头的行,尽管我既没有使用类似于的
运算符,也没有使用
%
通配符运算符。为什么

size
字段的类型为
VARCHAR

SELECT * FROM `articles` WHERE `form` = 'Depotplåster' AND `size` = '5'
-- this will compare the string 'size' with the string '5'


SELECT * FROM `articles` WHERE `form` = 'Depotplåster' AND `size` = 5
-- this will convert string 'size' to integer and then compare with the integer 5

字符串到整数的转换查找字符串开头的整数,并取最大整数,直到第一个非数字字符

select '5s4'=5, 's5'=5, '5'=5 -- =>1,0,1

您应该引用5,因为MySQL将表中的字符串转换为不带引号的int。

这是因为您使用的是数值和varchar数据之间的比较。MySQL将隐式地将您的列转换为
double
,结果是
5
。请参见以下简单的测试数据:

mysql> select * from test; +-----------------+ | name | +-----------------+ | 5 | | 5 and some crap | +-----------------+ 2 rows in set (0.00 sec) 将导致:

+----+-----+----------+ | 5d | 5sd | areEqual | +----+-----+----------+ | 5 | 5 | 1 | +----+-----+----------+ 1 row in set (0.00 sec) +----+-----+----------+ |5d | 5sd |相等| +----+-----+----------+ | 5 | 5 | 1 | +----+-----+----------+ 一行一组(0.00秒)
-如您所见,非重要部分刚刚被截断(如上面的警告消息中所述)

您是否尝试将5括在单引号内?我猜,因为您没有将
5
作为字符串(
'5'
)引用,所以您正在强制将列转换为整数,显然,
5x1 plåster
,当转换成整数时,是5。我有点惊讶mysql不会就额外的文本向您发出警告。谢谢。所以这只是我这边的一个简单错误。希望其他人能像我一样来这里学习:) mysql> select * from test where name = '5'; +------+ | name | +------+ | 5 | +------+ 1 row in set (0.00 sec) mysql> select * from test where name = 5; +-----------------+ | name | +-----------------+ | 5 | | 5 and some crap | +-----------------+ 2 rows in set, 1 warning (0.05 sec) +---------+------+-----------------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------------+ | Warning | 1292 | Truncated incorrect DOUBLE value: '5 and some crap' | +---------+------+-----------------------------------------------------+ 1 row in set (0.00 sec)
SELECT 
  CAST('5' AS DECIMAL) AS 5d, 
  CAST('5 and some crap' AS DECIMAL) AS 5sd, 
  CAST('5' AS DECIMAL) = CAST('5 and some crap' AS DECIMAL) AS areEqual;
+----+-----+----------+ | 5d | 5sd | areEqual | +----+-----+----------+ | 5 | 5 | 1 | +----+-----+----------+ 1 row in set (0.00 sec)