Postgres SQL内连接语法

时间:2015-12-09 08:59:44

标签: sql postgresql inner-join

有人可以在下面的SQL中解释内连接语法:

 CREATE TABLE dataset AS 
  SELECT property.id 
       , amount.band
       , amount."value"
 FROM property  
 INNER JOIN (locality INNER JOIN amount ON locality.code = amount.code) ON (property.band = amount.band) AND (property.id = locality."UniqueId")

为什么在第二次内连接之前定义了表locality?我从未遇到过这种奇怪的语法。

是否有更明确的方法来纠正同一个查询,以便有人能够轻松了解最新情况?

1 个答案:

答案 0 :(得分:2)

FROM property  
  INNER JOIN (locality INNER JOIN amount ON locality.code = amount.code)
     ON (property.band = amount.band) AND (property.id = amount."UniqueId")

相同
FROM property  
  INNER JOIN amount ON property.band = amount.band AND property.id = amount."UniqueId"
  INNER JOIN locality ON locality.code = amount.code

INNER JOIN时,您可以根据需要重新排序。

(JOIN locality的任何特定原因?你没有选择它的任何列。它是某种EXISTS,或者如果该表中有多个匹配的行,你想要多行返回吗?)