例子
賬戶表:
id | name
1 | Checking
2 | Visa
交易表:
date | description | amount | from_id | to_id
10-8 | payment | $100 | 1 | 2
題:
如何查詢 Transactions 表并獲取 from_id 和 to_id 列的名稱,它們都參考 Accounts 表中的 id 列?
使用上面的例子,我試圖回傳:
date | description | amount | from | to
10-8 | payment | $100 | Checking | Visa
uj5u.com熱心網友回復:
你必須加入它兩次:
select t.date, t.description, t.amount, a_from.name fromname, a_to.name toName
from transactions t
join accounts a_to on t.to_id = a_to.id
join accounts a_from on t.from_id = a_from.id
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/315215.html
標籤:sql PostgreSQL 加入
