Postgresql
如果兩個表之一上的記錄不存在,PostgreSQL JOIN SELECT null
我有 2 個表格,其中的數據如下:
tableA ======== 1 2 3 4 5 tableB ======= 1 3 5 6 8 9
使用此查詢:
select coalesce(tA.id, null) as id_A, coalesce(tB.id, null) as id_B from tableA tA inner join tableB tB on tA.id = tB.id
它目前輸出:
current result ============== 1|1 2|null 3|3 4|null 5|5
我的預期結果是:
expected result =============== 1|1 2|null 3|3 4|null 5|5 null|6 null|8 null|9
儘管沒有,我還是希望出現
6
,8
和9
值。我試過, ,但它不能給出我預期的結果。我懷疑我剛剛選擇的條款上有一些東西。tableB``tableA``LEFT JOIN``RIGHT JOIN``FULL OUTER JOIN``FROM``tableA
我怎樣才能得到預期的結果?
謝謝!
這是基本的PostgreSQL FULL OUTER JOIN子句
SELECT tA.id as id_A, tB.id as id_B FROM tableA tA FULL OUTER JOIN tableB tB ON tA.id = tB.id;
https://dbfiddle.uk/?rdbms=postgres_13&fiddle=508afb6b251572d45c01e6ec174da2ba
了解更多資訊:https ://www.postgresql.org/docs/current/queries-table-expressions.html
FULL OUTER JOIN 工作得很好:
SELECT a.id AS a_id, b.id AS b_id FROM tablea a FULL OUTER JOIN tableb b ON (a.id = b.id);