简体   繁体   English

在 Postgresql 中不使用 INNER JOIN 的地方

[英]WHERE not working with INNER JOIN in Postgresql

When I run:当我运行时:

SELECT * from actor inner join film_actor fa ON actor.actor_id = fa.actor_id

I get results.我得到结果。

But running:但是运行:

SELECT * from actor inner join film_actor fa WHERE actor.actor_id = fa.actor_id

It gives error:它给出了错误:

ERROR: syntax error at or near "WHERE"错误:“WHERE”处或附近的语法错误

As far as I understand, WHERE is correct usage in this case.据我了解,在这种情况下,WHERE 是正确的用法。

Any input would help.任何输入都会有所帮助。

As far as I understand, WHERE is correct usage in this case.据我了解,在这种情况下,WHERE 是正确的用法。

No, you mis-understood this.不,你误解了这一点。

The WHERE clause comes after all JOIN clauses, it can never be part of the JOIN condition . WHERE 子句出现所有 JOIN 子句之后,它永远不能成为 JOIN条件的一部分。

The general format of a JOIN is: JOIN 的一般格式是:

table_b JOIN table_b ON <some_condition>

there is no WHERE allowed in that part.该部分不允许WHERE The only variation to that are different types of joins ( left join , full join , right join ).唯一的变化是不同类型的联接( left join联接、 full join联接、 right join联接)。 The joins always follow the FROM clause.连接始终遵循 FROM 子句。 Only if all joins are fully specified ( including the join condition using ON ) you can start writing the WHERE clause.只有完全指定所有连接(包括使用ON的连接条件),您才能开始编写WHERE子句。

Ignoring sub-queries and derived tables for the moment, you can imagine the FROM clause as "one big thing", and only if that is complete you not allowed to write the WHERE clause.暂时忽略子查询和派生表,您可以将FROM子句想象为“一件大事”,并且只有当它完整时,您才允许编写WHERE子句。

Maybe you are thinking of the ancient, outdated and fragile implicit joins:也许你在想古老的、过时的和脆弱的隐式连接:

SELECT * 
from actor, 
     film_actor fa 
WHERE actor.actor_id = fa.actor_id

But using the explicit JOIN operator with the join condition in the ON clause is much better coding style.但是在ON子句中使用带有连接条件的显式JOIN运算符是更好的编码风格。 Don't get used to the outdated and ancient implicit joins.不要习惯过时和古老的隐式连接。

对于您想要的那种语法风格

SELECT * from actor, film_actor fa WHERE actor.actor_id = fa.actor_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM