我想在單個select查詢中將我的主表連接到PostgreSQL中的某些子表。我遇到了一個語法錯誤,我感覺我犯了一個可怕的錯誤,或者做了一些不允許的事情。代碼: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
我們能用子句中使用左聯接子或嵌套查詢,是否有更好的方法來做到這一點?
UPDATE1
嗯,我想補充一些更多的細節。我有三個像這樣的選擇查詢(具有唯一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
加入他們的行列。前兩個查詢已成功加入。雖然,加入第三個查詢我得到了語法錯誤。也許,我正在複雜的事情,因此我問是否有更好的方式來做到這一點。
'選擇ID,從價值,B'創建一個交叉連接A'和'B'之間'(這樣的橫'some_table'和'other_table'之間加入)。那真的是你想要的嗎? –
我已經添加了我的要求。 –