7.2.2. The WHERE Clause

左心房为你撑大大i 提交于 2020-02-27 15:03:57
7.2.2. The WHERE Clause
7.2.2.where子句
The syntax of the WHERE Clause is
where子句的语法如下:
 
WHERE search_condition
 
where search_condition is any value expression (see Section 4.2) that returns a value of type boolean.
search_condition可以是任何返回Boolean类型的值表达式(参见4.2节)。
 
After the processing of the FROM clause is done, each row of the derived virtual table is checked against the search condition. If the result of the condition is true, the row is kept in the output table,otherwise (i.e., if the result is false or null) it is discarded. The search condition typically references at least one column of the table generated in the FROM clause; this is not required, but otherwise the WHERE clause will be fairly useless.
在完成FROM子句的处理后,对照搜索条件检查派生虚拟表的每一行。如果条件的结果为true,则将该行保留在输出表中,否则(即结果为false或null)将其丢弃。搜索条件通常引用FROM子句中生成的表的至少一列; 这不是必需的,但不这样的话,WHERE子句将毫无用处。
 
Note
The join condition of an inner join can be written either in the WHERE clause or in the JOIN clause. For example, these table expressions are equivalent:
内联接的连接条件既可以写到where子句中,也可以写到join子句中。例如,以下表达式时一样的:
 
FROM a, b WHERE a.id = b.id AND b.val > 5
 
and:
和:
 
FROM a INNER JOIN b ON (a.id = b.id) WHERE b.val > 5
 
or perhaps even:
或者可以:
 
FROM a NATURAL JOIN b WHERE b.val > 5
 
Which one of these you use is mainly a matter of style. The JOIN syntax in the FROM clause is probably not as portable to other SQL database management systems, even though it is in the SQL standard. For outer joins there is no choice: they must be done in the FROM clause. The ON or USING clause of an outer join is not equivalent to a WHERE condition, because it results in the addition of rows (for unmatched input rows) as well as the removal of rows in the final result.
使用哪一个主要取决于自己的习惯。即使JOIN语法为SQL标准,但FROM子句中的JOIN语法也可能无法移植到其他SQL数据库管理系统中。对于外部联接,则别无选择:它们必须放在FROM子句中。外部联接的ON或USING子句不等同于WHERE条件,因为它会导致最终结果中既包含符合条件的也包含不符合条件的
 
Here are some examples of WHERE clauses:
以下为where子句的几个示例:
 
SELECT ... FROM fdt WHERE c1 > 5
SELECT ... FROM fdt WHERE c1 IN (1, 2, 3)
SELECT ... FROM fdt WHERE c1 IN (SELECT c1 FROM t2)
SELECT ... FROM fdt WHERE c1 IN (SELECT c3 FROM t2 WHERE c2 = fdt.c1 + 10)
SELECT ... FROM fdt WHERE c1 BETWEEN (SELECT c3 FROM t2 WHERE c2 = fdt.c1 + 10) AND 100
SELECT ... FROM fdt WHERE EXISTS (SELECT c1 FROM t2 WHERE c2 > fdt.c1)
 
fdt is the table derived in the FROM clause. Rows that do not meet the search condition of the WHERE clause are eliminated from fdt. Notice the use of scalar subqueries as value expressions. Just like any other query, the subqueries can employ complex table expressions. Notice also how fdt is referenced in the subqueries. Qualifying c1 as fdt.c1 is only necessary if c1 is also the name of a column in the derived input table of the subquery. But qualifying the column name adds clarity even when it is not needed. This example shows how the column naming scope of an outer query extends into its inner queries.
fdt为from子句选择的表。会忽略fdt中不符合where子句中条件的行。注意将标量子查询用作值表达式的方式。就像任何其他查询一样,子查询可以使用复杂的表表达式。还要注意在子查询中如何引用fdt。仅当c1也是子查询的派生输入表中的列名时,才有必要将c1限定为fdt.c1。但是,限定列名即使在不需要时也可以增加清晰度。本示例说明外部查询的列命名如何在内部查询中使用。
 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!