[英]why does sql = FALSE always return true
No matter what I do in MySQL 5.0 such as 无论我在MySQL 5.0中做了什么,比如
SELECT 'test' = FALSE
SELECT '' = FALSE
I always get a 1 back in SQL. 我总是在SQL中得到1分。 What is the reason for that?
这是什么原因? I was expecting a 0 or FALSE
我期待0或FALSE
EDIT adding context to the questions. 编辑为问题添加上下文。 This is how the problem came about, it happened that $name inadvertently became false making this join always pass, then I wondered why this works.
这就是问题的出现,碰巧$ name无意中变得虚假,使得这个连接总是通过,然后我想知道为什么这个有效。
SELECT a.id
FROM user a
INNER JOIN inventory b ON b.user_id = a.id AND b.name = $name
In MySql FALSE is a constant literal which is always evaluated as 0. 在MySql中, FALSE是一个常量文字,总是被计算为0。
So you are checking if 'test' = 0
or if '' = 0
, and since you are comparing a string with an integer, MySql will try to cast the string to an integer. 因此,您正在检查
'test' = 0
或者'' = 0
,并且由于您要将字符串与整数进行比较,MySql将尝试将字符串转换为整数。
If you try this query: 如果您尝试此查询:
SELECT 'test' = FALSE
it will return 1 (TRUE) because 'test' will be converted to 0, while if you try this: 它将返回1(TRUE),因为'test'将转换为0,而如果您尝试这样:
SELECT '1test' = FALSE
it will return 0 (FALSE) because '1test' will be converted to 1. 它将返回0(FALSE),因为'1test'将转换为1。
This has to do with MySQL's implicit conversion when using comparison operators (ie =
) 这与使用比较运算符时的MySQL 隐式转换有关 (即
=
)
Taken from the docs: 取自文档:
Strings are automatically converted to numbers and numbers to strings as necessary.
根据需要,字符串会自动转换为数字和数字。
So, in your case: 所以,在你的情况下:
'test'
gets converted to 0
'test'
转换为0
FALSE
is 0
FALSE
为0
0 = 0
is TRUE
. 0 = 0
为TRUE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.