我有4臺這樣的PostgreSQL的 - 在聲明中有一個表從許多不同的
table a:
art_id | name | surname
-------+-------+--------
1 | John | McA
2 | Alex | McB
3 | Juddy | McC
table b:
art_id is a foreign key to table a
diff_id is foreign key to table c
art_id | title | diff_id
-------+-------+--------
1 | smth | 1.1
2 | else | 1.2
3 | here | 1.3
table c:
class is foreign key to table d
class | date | diff_id
-------+-------+--------
a | 01.02 | 1.1
b | 02.03 | 1.2
c | 03.04 | 1.3
table d:
class | deputy|
-------+-------+
a | John |
b | Marc |
c | Sophie|
我想寫一個postresql語句有這樣一個表:
table result:
| title | deputy |
+-------+--------+
| smth | John |
| else | Marc |
| here | Sophie |
我我試圖通過WITH AS statement bun來做到這一點,因爲我被卡住了。 在聲明中創建遞歸表後,我試圖通過執行LEFT JOIN語句來解決該問題,但它將結果表中的記錄相乘。
謝謝你的幫忙!
爲什麼你需要WITH或遞歸? b,c和d之間不會有簡單的連接嗎? –
我沒有嘗試過。我需要爲特定的art_id收集diff_id,存儲diff_id以從表c中獲取類,並將該類存儲爲最終從表d獲取副本。如果你知道如何做得更快,更容易隨時給我一個建議:)! – axeMaltesse
'選擇b.art_id,d.deputy從b連接c上b.diff_id = c.diff_id連接d上c.class = d.class'浮現在腦海中 –