Where子句返回所有行时的错误值? [英] False value in Where Clause Returns All Rows?
问题描述
我有一个如下查询:
`SELECT id, username FROM table_name WHERE username=0`
当我运行此查询时,MySQL返回table_name中的所有行.另外,如果用0代替false,我将得到相同的结果.如果我使用null或空字符串,则不会返回任何行(按预期).
When I run this query MySQL returns all rows in table_name. Additionally, if I substitute 0 for false I get the same results. If I use null or an empty string then I get no rows returned (as expected).
如果有所不同,用户名列为varchar(50).
The username column is varchar(50) if it makes a difference.
然后我的问题是这个:
为什么在该查询中放入0或false会返回表中的所有行?这是MySQL设置吗?
Why does putting 0 or false in that query return all rows in the table? Is this a MySQL setting?
这让我有些担心,因为我一直在假设上述查询将不返回任何行(在这种情况下)的情况下工作,我想知道这是否在我的应用程序中的其他地方发生,以及它可能产生什么意外的结果./p>
This worries me a little as I've been operating under the assumption that the above query would return no rows (in this particular case) and I wonder if this happening elsewhere in my application and what unintended consequences it might have.
推荐答案
用户名被强制转换为0的整数以进行比较. 0 = 0的计算结果为true,所以好像这里根本没有WHERE子句.如果您要获取非空的空白用户名,则可以使用username=''
.
username is being cast to an int of 0 to make the comparsion. 0=0 evaluates to true, so it's like there's no WHERE clause at all here. You could use username=''
if you're trying to get a non-null blank username.
这篇关于Where子句返回所有行时的错误值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!