PostgreSQL:左联接中出错

PostgreSQL:左联接中出错,sql,postgresql,left-join,Sql,Postgresql,Left Join,我试图在一个select查询中将我的主表连接到PostgreSQL中的一些子表。我有一个语法错误,我觉得我犯了一个可怕的错误或做了一些不允许的事情。守则: Select id, length, other_stuff from my_table tbl1 Left join ( Select id, height from my_table2 tbl2) tbl2 using (id) left join -- I get syntax error here ( With a as (selec

我试图在一个select查询中将我的主表连接到PostgreSQL中的一些子表。我有一个语法错误,我觉得我犯了一个可怕的错误或做了一些不允许的事情。守则:

Select
id,
length,
other_stuff
from my_table tbl1
Left join
(
Select
id,
height
from my_table2 tbl2) tbl2 using (id)
left join
-- I get syntax error here
(
With a as (select id from some_table),
     b as (Select value from other_table)
Select id, value from a, b) tbl3 using (id)
order by tbl1.id
我们可以在左联接子查询或嵌套查询中使用WITH子句吗?有更好的方法吗

更新1

嗯,我想补充一些细节。我有三个类似这样的select查询(具有唯一ID),我想根据ID将它们连接起来

问题1:

With a as (Select id, my_other records... from postgres_table1)
     b as (select id, my_records... from postgres_table2)
     c as (select id, my_record.. from postgres_table3, b)
     Select
           id,
           my_records
     from a left join c on some_condtion_with_a
     order by 1
第二个问题:

Select
      id, my_records
from
    (
    multiple_sub_queries_by_getting_records_from_c
    )
第三个问题:

With d as (select id, records.. from b),
     e as (select id, records.. from d),
     f as (select id, records.. from e)
select
      id,
      records..
from f

我尝试使用
left join
加入他们。前两个查询已成功联接。在加入第三个查询时,我得到了语法错误。也许,我把事情复杂化了,所以我问有没有更好的方法来做。

没有测试过,但我想你需要这个。尝试:

with a as (select id from some_table),
b as (Select value from other_table)
Select
id,
length,
other_stuff
from my_table tbl1
Left join
(
    Select
    id,
    height
    from my_table2 tbl2
) 
tbl2 using (id)
left join
(
    Select id, value from a, b
) 
tbl3 using (id)
order by tbl1.id

没有测试,但我想你需要这个。尝试:

with a as (select id from some_table),
b as (Select value from other_table)
Select
id,
length,
other_stuff
from my_table tbl1
Left join
(
    Select
    id,
    height
    from my_table2 tbl2
) 
tbl2 using (id)
left join
(
    Select id, value from a, b
) 
tbl3 using (id)
order by tbl1.id

我只见过/使用过以下格式的:

WITH 
  temptablename(columns) as (query),
  temptablename2(columns) as (query),
  ...
  temptablenameX(columns) as (query)

SELECT ...
i、 他们先来

如果使用缩进来描述嵌套级别,您可能会发现编写查询更容易。我喜欢在一个缩进级别上选择GROUPBY ORDERBY的位置,然后在etc上使用tablename internal JOIN进行缩进:

SELECT
  column
FROM
  table
  INNER JOIN
  (
     SELECT subcolumn FROM subtable WHERE subclause
  ) myalias
  ON
    table.id = myalias.whatever
WHERE
  blah
每次嵌套层时组织缩进确实很有帮助。通过使“表或数据块(如表(即子查询))中的所有内容缩进相同的数量,您可以很容易地看到数据库应该检索的概念顺序

将您的WISS移到语句的顶部,您当然仍将使用子查询中的别名

看看你的查询,你的子查询没有什么意义。。您不需要对数据进行任何分组或特别复杂的处理,只需选择一个ID和另一列,然后将其加入。如果不这样做,您的查询将更简单:

SELECT
  column
FROM
  table
  INNER JOIN
  (
     SELECT subcolumn FROM subtable WHERE subclause
  ) myalias
  ON
    table.id = myalias.whatever
WHERE
  blah
相反,请执行以下操作:

SELECT
  column
FROM
  table
  INNER JOIN
  subtable
  ON
    table.id = subtable.id
WHERE
  blah

我只见过/使用过以下格式的:

WITH 
  temptablename(columns) as (query),
  temptablename2(columns) as (query),
  ...
  temptablenameX(columns) as (query)

SELECT ...
i、 他们先来

如果使用缩进来描述嵌套级别,您可能会发现编写查询更容易。我喜欢在一个缩进级别上选择GROUPBY ORDERBY的位置,然后在etc上使用tablename internal JOIN进行缩进:

SELECT
  column
FROM
  table
  INNER JOIN
  (
     SELECT subcolumn FROM subtable WHERE subclause
  ) myalias
  ON
    table.id = myalias.whatever
WHERE
  blah
每次嵌套层时组织缩进确实很有帮助。通过使“表或数据块(如表(即子查询))中的所有内容缩进相同的数量,您可以很容易地看到数据库应该检索的概念顺序

将您的WISS移到语句的顶部,您当然仍将使用子查询中的别名

看看你的查询,你的子查询没有什么意义。。您不需要对数据进行任何分组或特别复杂的处理,只需选择一个ID和另一列,然后将其加入。如果不这样做,您的查询将更简单:

SELECT
  column
FROM
  table
  INNER JOIN
  (
     SELECT subcolumn FROM subtable WHERE subclause
  ) myalias
  ON
    table.id = myalias.whatever
WHERE
  blah
相反,请执行以下操作:

SELECT
  column
FROM
  table
  INNER JOIN
  subtable
  ON
    table.id = subtable.id
WHERE
  blah

你把事情复杂化了。无需使用派生表进行外部联接
my_table2
。而且不需要CTE加上派生表来连接
tbl3
别名:

Select id,
       length,
       other_stuff
from my_table tbl1
  Left join my_table2 tbl2 using (id) 
  left join (
    select st.id, ot.value
    from some_table st
       cross join other_table ot
  ) tbl3 using (id)
order by tbl1.id;

这假设您使用
Select id,a,b中的值创建的交叉联接是预期的

你把事情复杂化了。无需使用派生表进行外部联接
my_table2
。而且不需要CTE加上派生表来连接
tbl3
别名:

Select id,
       length,
       other_stuff
from my_table tbl1
  Left join my_table2 tbl2 using (id) 
  left join (
    select st.id, ot.value
    from some_table st
       cross join other_table ot
  ) tbl3 using (id)
order by tbl1.id;

这假设您使用
Select id,a,b中的值创建的交叉联接是预期的

按照相同的模式重新更新您的需求

查找
——我的评论

With a as (Select id, my_other records... from postgres_table1)
     b as (select id, my_records... from postgres_table2)
     c as (select id, my_record.. from postgres_table3, b)
     d as (select id, records.. from b),
     e as (select id, records.. from d),
     f as (select id, records.. from e)

SELECT * FROM
(
    --your first 
    Select
      id,
      my_records
    from a left join c on some_condtion_with_a

) Q1
LEFT OUTER JOIN
(
    --your second
    Select
      id, my_records
    from
    (
      multiple_sub_queries_by_getting_records_from_c
    )

) Q2
ON Q1.XXXX = Q2.XXXX --fill this in !!!!!!!!!!!!!!!!!!!

LEFT OUTER JOIN
(
    --your third
    select
      id,
      records..
    from f

) Q3
ON QX.XXXXX = Q3.XXXX --fill this in !!!!!!!!!!!!!!!!!!!
它会起作用,但它可能不是最漂亮或最必要的SQL安排。正如我和HWNN都说过的,你可以重写很多这样的查询,你只需要用..做一些简单的选择。。但它们可能非常简单,数据库优化器也可以看到这一点,并在运行查询时为您重新编写查询


只需记住清楚地编码,并将缩进很好地布局,以防止它变成一个巨大的、无法维护的、无法调试的意大利面一团糟

按照相同的模式更新您的需求

查找
——我的评论

With a as (Select id, my_other records... from postgres_table1)
     b as (select id, my_records... from postgres_table2)
     c as (select id, my_record.. from postgres_table3, b)
     d as (select id, records.. from b),
     e as (select id, records.. from d),
     f as (select id, records.. from e)

SELECT * FROM
(
    --your first 
    Select
      id,
      my_records
    from a left join c on some_condtion_with_a

) Q1
LEFT OUTER JOIN
(
    --your second
    Select
      id, my_records
    from
    (
      multiple_sub_queries_by_getting_records_from_c
    )

) Q2
ON Q1.XXXX = Q2.XXXX --fill this in !!!!!!!!!!!!!!!!!!!

LEFT OUTER JOIN
(
    --your third
    select
      id,
      records..
    from f

) Q3
ON QX.XXXXX = Q3.XXXX --fill this in !!!!!!!!!!!!!!!!!!!
它会起作用,但它可能不是最漂亮或最必要的SQL安排。正如我和HWNN都说过的,你可以重写很多这样的查询,你只需要用..做一些简单的选择。。但它们可能非常简单,数据库优化器也可以看到这一点,并在运行查询时为您重新编写查询


只需记住清楚地编码,并将缩进部分巧妙地布置好,以防止它变成一个巨大的、无法维护的、无法调试的意大利面团

Select id,value from a,b
a
b
之间创建交叉连接(因此
某些表
其他表
)。这真的是你想要的吗?我特别添加了我的要求。
Select id,value from a,b
a
b
之间创建了一个交叉连接(因此
一些表和
其他表之间的交叉连接
)。这真的是你想要的吗?我特别添加了我的要求。请参见编辑。我试图更详细地描述我的情景。总之,我想将第二次和第三次查询的结果与第一次查询的结果合并,但在我的结构中有多个子查询和派生表。请参阅编辑。我试图更详细地描述我的情景。总之,我想将第二次和第三次查询的结果与第一次查询的结果连接起来,但是在我的结构中有多个子查询和派生表。非常感谢您提供的详细信息。我已经添加了我想要加入的三个查询的结构。我的代码中有多个嵌套查询和分组依据。非常感谢您提供的详细信息。我已经添加了我想要加入的三个查询的结构。有多个嵌套队列