如何在同一个字段中查询2列?

时间:2013-02-19 22:44:51

标签: sql

假设我有2个表:

USER(uid, uname);
ITEM(iid, description);
TRANSACTION(buyer, seller, item);

transaction.buyertransaction.seller引用user.uidtransaction.item引用item.iid

如何查询交易的买方和卖方的名称?

1 个答案:

答案 0 :(得分:3)

您需要两个联接:

select t.*, b.uname as buyerName, s.uname as sellerName
from transaction t join
     user b
     on t.buyer = b.uid join
     user s
     on t.seller = s.uid