Mysql其中1 = 0混乱 [英] Mysql where 1= 0 confusion

查看:61
本文介绍了Mysql其中1 = 0混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在简单的SQL书籍中,如果我使用where 1 = 0,则有一部分代码提到,我可以安全地添加OR条件,如:

In simply sql book, there is portion of code mentioning if I use where 1 = 0, i can safely add an OR conditions like:

WHERE
   1=0
   OR name LIKE '%Toledo%'
   OR billaddr LIKE '%Toledo%'
   OR shipaddr LIKE '%Toledo%'

我不明白为什么我使用where 1 = 0时可以安全地添加OR语句

I didnt get it why when i use where 1 = 0 i can safely add an OR statement

我可以只使用下面的内容吗?

Can i just use something like below?

WHERE
   1=1
   OR name LIKE '%Toledo%'
   OR billaddr LIKE '%Toledo%'
   OR shipaddr LIKE '%Toledo%'

我知道where 1=1是添加And语句的快捷方式.
我了解这是因为where 1 =1将返回true.我也可以将其用于Or语句吗?
Istill没有得到零件where 1 = 0
任何解释都很好.

I understand that where 1=1 is a shortcut to add an And statement.
I understand that because where 1 =1 will return true.Can i use it for Or statement too?
Istill didnt get the part where 1 = 0
Any explaination would be great.

推荐答案

这取决于您正在处理的布尔运算的类型.如果要添加可变数量的AND语句,则可以使用始终为true的语句,例如1 = 1.另一方面,如果要对OR语句执行相同的操作,则应使用评估结果为false的语句,例如1 = 0.

This depends on the type of boolean operations you're working on. If you want to add a variable number of AND statements, then you use a statement that invariably evaluates to true, such as 1 = 1. On the other hand, if you want to do the same with OR statements, then you should use a statement that evaluates to false, such as 1 = 0.

假设您有一个带不确定的真值的布尔变量x(它可能为true,也可能为false.您不知道.)现在,如果您找到x AND false的值,您将不管x的值是什么,都获取false.

Let's say you have a boolean variable x with an indeterminate truth value (it might be true, or it might be false. You don't know.) Now, if you find the value of x AND false, you get false, regardless of what the value of x is.

另一方面,如果您查看x OR true,您会得到true.同样,这与x的真值无关.

On the other hand, if you look at x OR true, you'll get true. Again, this is regardless of the truth value of x.

在您的语句中,您希望硬编码的值对查询的逻辑没有影响.由于false OR a OR b OR c在逻辑上等效于a OR b OR c,因此硬编码语句无效.在另一种情况下,true AND a AND b AND c等同于a AND b AND c.

In your statement, you want the hard-coded value to have no effect on the logic of the query. Since false OR a OR b OR c is logically equivalent to a OR b OR c, the hard-coded statement has no effect. In the other case, true AND a AND b AND c is equivalent to a AND b AND c.

这篇关于Mysql其中1 = 0混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆