簡體   English   中英

PostgreSQL:左連接錯誤

[英]PostgreSQL: Error in left join

我試圖在一個選擇查詢中將我的主表連接到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子句嗎?有更好的方法嗎?

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加入它們。 前兩個查詢已成功加入。 雖然,加入第三個查詢我得到了語法錯誤。 也許,我使事情變得復雜,因此我問有沒有更好的方法來做到這一點。

沒有經過測試,但我認為你需要這個。 嘗試:

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:

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

SELECT ...

即他們是第一位的

如果使用縮進來描述嵌套級別,您可能會發現編寫查詢更容易。 我喜歡在一個縮進級別創建SELECT FROM WHERE GROUPBY ORDERBY,然后將tablename INNER JOIN ON等更多縮進:

SELECT
  column
FROM
  table
  INNER JOIN
  (
     SELECT subcolumn FROM subtable WHERE subclause
  ) myalias
  ON
    table.id = myalias.whatever
WHERE
  blah

每次嵌套圖層時組織縮進確實有幫助。 通過使“表或數據塊(如表(即子查詢)”的所有內容縮進相同的數量,您可以輕松地看到數據庫應該檢索的名義順序

將您的WITH移動到語句的頂部,當然您仍將在子子查詢中使用別名

查看您的查詢,您的子查詢中沒有太多要點。您不進行任何分組或特別復雜的數據處理,只需選擇一個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, value from a, b創建的交叉連接Select id, value from a, b是預期的。

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

尋找 - --my comments

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安排。 正如i和HWNN所說,你可以重寫很多這些查詢,你只是在你的WITH中做一些簡單的選擇..但很可能它們很簡單,數據庫優化器也可以看到這個並為你重新創建查詢當它運行它

只要記住要清楚地編碼,並妥善地打下你的縮進,以阻止它變成一個巨大的,不可維護的,不可摧毀的意大利面條

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM