[英]Retrieve columns from the same table with the same foreign keys but different values
[英]Retrieve same column of a table for 2 different columns in another table
我有 2 個表“用戶”和“交易”。 用戶表包含用戶 ID 和名稱。 Transactions 表包含 senderid、receiverid 和金額。 我想得到發送者和接收者的名字。 senderid 和receiverid 是users.userid 的外鍵
您需要多次join
users
表:
select s.name, r.name, t.amount
from transactions t
join users s on t.senderid = s.userid
join users r on t.senderid = r.userid
如果transactions
表中存在users
表中不存在的用戶 ID,則需要改用outer join
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.