在 SQL 查询中使用 1=2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15158811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:59:15  来源:igfitidea点击:

Use of 1=2 in a SQL query

sql

提问by Kings

Someone please explain the meaning of '1=2'in the below SQL query.

有人请解释下面 SQL 查询中'1=2'的含义。

SELECT E.EmpID,
       E.EmpName,
       Country = CASE
                   WHEN T.Active = 'N'
                        AND 1 = 2 THEN 'Not Working Anymore'
                   ELSE C.Country_Name
                 END,
       T.Contract_No
FROM   Employees E (nolock)
       INNER JOIN Contract T
         ON T.Contract_No = E.Contract_No
       LEFT JOIN Country C (nolock)
         ON E.Country_ID = C.Country_ID 

thanks

谢谢

EDIT:- Corrected the slight mistake existed in the example SQL query given by me. @ ALL :- The query mentioned here is an example version of a big working query on which I have to reoslve something. I have created a sample scenario of SQL query for the sake of simplicity of question.

编辑:-更正了我给出的示例 SQL 查询中存在的轻微错误。@ ALL :- 此处提到的查询是一个大型工作查询的示例版本,我必须对其进行重新处理。为了简单起见,我创建了一个 SQL 查询示例场景。

采纳答案by Yaqub Ahmad

when T.Active = 'N' and 1=2 then 'Not Working Anymore' 

Simple, the above condition will never become true. So the result will always be C.Country_Name

很简单,上面的条件永远不会变成true. 所以结果永远是C.Country_Name

回答by Ian_T

There is a good use for this 1=2part of the WHEREclause if you are creating a table from another, but you don't want to copy any rows. For example:

如果您要从另一个表创建表,但不想复制任何行1=2,则WHERE子句的这一部分很有用。例如:

CREATE TABLE ABC_TEMP AS
    SELECT * FROM ABC WHERE 1=2;

回答by Pieter Geerkens

It is a common trick used in dynamic construction of SQL filter clauses. This allows the automated construction of "T.Active = 'N' and" with no check needed for a following clause, because "1=2" will always be appended.

这是动态构造 SQL 过滤器子句时常用的技巧。这允许自动构建“T.Active = 'N' and”而无需检查后续子句,因为“1=2”将始终被附加。

Update:Whether 1=1 or 1=2 is used depends on whether conjunctive or disjunctive normal form is supposed to be used in building the automated clauses. In this case, there seems to have been a mismatch of design and implementation.

更新:使用1=1 还是 1=2 取决于在构建自动子句时应该使用连接式还是分离式范式。在这种情况下,设计和实现似乎不匹配。

Update 2I believe most developers prefer conjunctive normal form, with major terms joind by AND, but disjunctive normal form is equal in expressive power and size of code.

更新 2我相信大多数开发人员更喜欢连接范式,主要术语由 AND 连接,但析取范式在表达能力和代码大小方面是相同的。

回答by bonsvr

It corresponds to a FALSE argument.

它对应于 FALSE 参数。

For example ;

例如 ;

select * from TABLE where 1=2

returns zero rows.

返回零行。

回答by ssilas777

Use WHERE 1=2if you don'twant to retrieve any rows,

使用WHERE 1=2,如果你想要检索任何行,

As 1=2is always false.

由于1=2始终false

回答by Dan Bracuk

adding and 1=2 will cause that case to always return false. To find out why it's there, ask the person who put it there.

添加和 1=2 将导致这种情况总是返回 false。要找出它为什么在那里,请询问将它放在那里的人。

I suspect it was put there so the author could force the first condition to be false and then he forgot to remove it.

我怀疑它放在那里是为了作者可以强制第一个条件为假,然后他忘记删除它。

回答by LEARNDATAsCI

1 = 2 means that we are giving a condition that will always be false; therefore no records will show ('NULL') for your rows...

1 = 2 表示我们给出的条件永远为假;因此不会为您的行显示 ('NULL') 记录...

ie Create table empt_tgt AS Select empno, ename, job, mgr, sal WHERE 1=2;

即创建表 empt_tgt AS Select empno, ename, job, mgr, sal WHERE 1=2;

then assuming that empt_tgt has records for all those columns when we perform the following statement: SELECT * FROM empt_tgt

然后假设当我们执行以下语句时 empt_tgt 具有所有这些列的记录:SELECT * FROM empt_tgt

EMPT_TGT will be null ; meaning we will only see the column name empno, ename, job, mgr,sal no data...

EMPT_TGT 将为空;这意味着我们只会看到列名 empno、ename、job、mgr、sal 没有数据...

回答by Dumitrescu Bogdan

I would guess that is a debug script. It is there to always return the negative part of the case. Probably on release that part is taken out.

我猜这是一个调试脚本。它总是返回案例的负面部分。可能在发布时,该部分被取出。

回答by agohil

I have found this in several bits of code at my company. In our case it generally gets left in as DEBUG code by mistake. Developers could use it as a place holder which looks like the case in your example.

我在我公司的几段代码中发现了这一点。在我们的例子中,它通常会被错误地留在调试代码中。开发人员可以将其用作占位符,这与您的示例中的情况类似。

回答by Tono Darmodjo

People use 1=2 to check if their code is syntactically correct without the code performing anything. For example if you have a complicated UPDATE statement and you want to check if the code is correct without updating anything.

人们使用 1=2 来检查他们的代码在语法上是否正确,而无需代码执行任何操作。例如,如果您有一个复杂的 UPDATE 语句,并且您想在不更新任何内容的情况下检查代码是否正确。