WHERE 1 在 MySQL 查询中的重要性

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

Importance of WHERE 1 in MySQL queries

mysql

提问by Brian

When making a MySQL query with no WHERE constraints, most people use WHERE 1 in the query. However, omitting WHERE 1 does not influence the query. Is there a difference between the two? Is one considered to be the best practice?

在进行没有 WHERE 约束的 MySQL 查询时,大多数人在查询中使用 WHERE 1。但是,省略 WHERE 1 不会影响查询。两者有区别吗?一种被认为是最佳实践吗?

回答by Eric Petroelje

I don't think it's a matter of best practice, but people sometimes use it to make building dynamic queries a bit easier.

我不认为这是最佳实践的问题,但人们有时会使用它来使构建动态查询更容易一些。

string sql = "SELECT * FROM mytable WHERE 1 ";
if ( somecondition ) {
   sql += "AND somefield = somevalue ";
}

if ( someothercondition ) {
   sql += "AND someotherfield = someothervalue ";
}

... etc

Without the WHERE 1in there I would need to check in each ifblock whether I needed to put in a WHEREor an AND.

如果没有WHERE 1在那里,我需要检查每个if块是否需要放入 aWHEREAND.

回答by cletus

It's not necessary. 99.9% of the time it just means the query was dynamically constructed and putting in WHERE 1leads to simpler logic for dynamic conditional clauses, meaning you can just keep adding AND id = 3to the end of the query and it won't break the syntax. If you don't have WHERE 1you have to worry about whether or not there's a WHEREclause and whether to prepend your condition with ANDor not.

这不是必需的。在 99.9% 的情况下,它只是意味着查询是动态构造的,并且放入WHERE 1会导致动态条件子句的逻辑更简单,这意味着您可以继续添加AND id = 3到查询的末尾,而不会破坏语法。如果您没有,则WHERE 1必须担心是否有WHERE子句以及是否在您的条件之前加上AND

So it's just laziness basically.

所以基本上只是懒惰。

回答by paxdiablo

It's one less corner case for automated code generators and other SQL statement manipulators. By starting the filtering section with where 1(or where 1 = 1), your automated code generator can just add new filters with the andprefix.

对于自动代码生成器和其他 SQL 语句操纵器来说,这是一种较少的极端情况。通过使用where 1(或where 1 = 1)开始过滤部分,您的自动代码生成器可以添加带有and前缀的新过滤器。

Otherwise you tend to end up with code like:

否则,您往往会得到如下代码:

query = "select * from tbl"
sep = " where "
foreach clause in all_clauses:
    query = query + sep + clause
    sep = "and "

which is not as clean as:

这不像:

query = "select * from tbl where 1 = 1"
foreach clause in all_clauses:
    query = query + " and " + clause

It should make verylittle difference to any decent DBMS since the execution engine should strip out those sort of clauses before the query is executed.

它应该非常小的差异,以任何像样的DBMS,因为在执行查询之前执行引擎应该去掉这些类型的条款。

Whether it's best practice or not depends on whether you'd rather have "cleaner" code in your query generators, or whether you'd rather have the company DBAs trying to track you down and beat you to death for using such silly clauses :-) Of course, if you're using MySQL, you may bethe DBA so that may not be a problem.

这是否是最佳实践取决于您是否希望在查询生成器中使用“更干净”的代码,或者您是否希望公司 DBA 试图追踪您并因使用此类愚蠢的条款而将您打死:- ) 当然,如果您使用 MySQL,您可能DBA,所以这可能不是问题。