drum
drum

Reputation: 5651

How to query into 2 columns the same field?

Suppose I have have 2 tables:

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

transaction.buyer and transaction.seller references user.uid. transaction.item references item.iid

How can I query the name of the buyer and seller of a transaction?

Upvotes: 1

Views: 58

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269643

You need two joins:

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

Upvotes: 3

Related Questions