False value in Where Clause Returns All Rows?

≯℡__Kan透↙ 提交于 2019-12-23 09:27:30

问题


I have a query that looks like this:

`SELECT id, username FROM table_name WHERE username=0`

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).

The username column is varchar(50) if it makes a difference.

My question then is this:

Why does putting 0 or false in that query return all rows in the table? Is this a MySQL setting?

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.


回答1:


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.



来源:https://stackoverflow.com/questions/6098875/false-value-in-where-clause-returns-all-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!