oracle 具有像 where 0=0 这样的条件的确切含义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18308678/
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-19 01:52:45  来源:igfitidea点击:

Whats the exact meaning of having a condition like where 0=0?

sqloracleprocedures

提问by user2622662

I'm having a bit confusion in using a statement like "where 0=0" in Oracle procedures? Can someone please clarify it? Even though we do it for dynamic selection, why do we add this statement even though we append the actual condition in the query? Will this where condition make any difference to the result set?.. I went through How can I Select all rows where column contain any words of a string? but I dint exactly understand the reason for using "where 0=0". Can some one please give me the proper reason for using such a condition?..

在 Oracle 过程中使用“where 0=0”之类的语句时,我有点困惑?有人可以澄清一下吗?即使我们这样做是为了动态选择,为什么即使我们在查询中附加了实际条件,我们还要添加这个语句?这个 where 条件对结果集有什么影响吗?.. 我经历了 如何选择列包含任何字符串单词的所有行?但我完全理解使用“where 0=0”的原因。有人可以给我使用这种条件的适当理由吗?..

Thanks in Advance..;)

提前致谢..;)

采纳答案by steve godfrey

When using dynamic sql, extra clauses may need to be added, depending upon certain conditions being met. The 1=1 clause has no meaning in the query ( other than it always being met ), its only use is to reduce the complexity of the code used to generate the query in the first place.

使用动态 sql 时,可能需要添加额外的子句,具体取决于是否满足某些条件。1=1 子句在查询中没有任何意义(除了它总是被满足),它的唯一用途是首先降低用于生成查询的代码的复杂性。

E.g. This pseudo code

例如这个伪代码

DECLARE
v_text VARCHAR2(2000) := 'SELECT * FROM table WHERE 1=1 ';
BEGIN

    IF condition_a = met THEN
    v_text := v_text ||' AND column_1 = ''A'' ';
    END IF;

    IF condition_b = also_met THEN
    v_text := v_text ||' AND column_2 = ''B'' ';
    END IF;

execute_immediate(v_text);

END;

is simpler than the pseudo code below, and as more clauses were added, it would only get messier.

比下面的伪代码更简单,并且随着添加更多子句,它只会变得更加混乱。

DECLARE
v_text VARCHAR2(2000) := 'SELECT * FROM table  ';
BEGIN

    IF condition_a = met THEN
    v_text := v_text ||' WHERE column_1 = ''A'' ';
    END IF;

    IF condition_b = also_met AND 
       condition_a != met THEN
    v_text := v_text ||' WHERE column_2 = ''B'' ';
    ELSIF condition_b = also_met AND 
       condition_a = met THEN
    v_text := v_text ||' AND column_2 = ''B'' ';
    END IF;

execute_immediate(v_text);

END;

回答by Dmitry Bychenko

We use 0 = 0or, usually, 1 = 1as a stub:

我们使用0 = 0或者,通常,1 = 1作为一个存根

select *
  from My_Table
 where 1 = 1

So when you write filters you can do it by adding/commenting out single lines:

因此,当您编写过滤器时,您可以通过添加/注释单行来实现

-- 3 filters added
select *
  from My_Table
 where 1 = 1
   and (Field1 > 123) -- 1st
   and (Field2 = 456) -- 2nd 
   and (Field3 like '%test%') -- 3d

Next version, say, will be with two filters removed:

例如,下一个版本将删除两个过滤器:

-- 3 filters added, 2 (1st and 3d) removed
select *
  from My_Table
 where 1 = 1
   -- and (Field1 > 123) -- <- all you need is to comment out the corresponding lines
   and (Field2 = 456)
   -- and (Field3 like '%test%')

Now let's restore the 3d filter in very easy way:

现在让我们以非常简单的方式恢复 3d 过滤器:

-- 3 filters added, 2 (1st and 3d) removed, then 3d is restored
select *
  from My_Table
 where 1 = 1
   -- and (Field1 > 123) 
   and (Field2 = 456)
   and (Field3 like '%test%') -- <- just uncomment

回答by aleroot

It is usually used when you need to concatenate a String of the SQL Query, so you write the first part :

通常在需要连接 SQL Query 的 String 时使用,因此您编写第一部分:

SELECT * FROM table WHERE 1=1 

and then if some condition is true you can append more clause, otherwise leaving the query as it is, it will run without errors ...

然后如果某些条件为真,您可以附加更多子句,否则将查询保持原样,它将运行而不会出错......

It is generally used to add more clause at runtime appending directly to the string of the query.

它通常用于在运行时添加更多子句,直接附加到查询的字符串。

回答by Harshit

This is always true Condition i.e. '0' will always be equal to '0'. Which means your condition will always be executed.

这始终是真实的条件,即“0”将始终等于“0”。这意味着您的条件将始终被执行。

Some people use this for ease in debugging of a query. They will put it in where clause and rest conditions with AND clause so that for checking purpose they can comment the unnecessary condition.

有些人使用它来轻松调试查询。他们会将它放在 where 子句中,并使用 AND 子句将其放在其余条件中,以便为了检查目的,他们可以注释不必要的条件。

For ex

对于前任

SELECT * from 
TABLE
WHERE 1=1
AND condition1
AND condition 2
.....

.

.

回答by MoneyPot

1=1 is also useful when you want join condition to be always true. For example something like that (adds b.value to all rows):

当您希望连接条件始终为真时,1=1 也很有用。例如类似的东西(将 b.value 添加到所有行):

select a.code, a.name, b.value
  from tableA a
  LEFT JOIN (SELECT MAX(value) AS value
               FROM tableB) b
    ON 1 = 1;