假设我有一个包含两列的table_1:buyer_id和seller_id。我有另一个表table_2,有两列:person_id,city。我的目标是一个包含四列的表格:buyer_id,buyer_city,seller_id,seller_city。
我知道我可以在那里找到像
这样的东西SELECT a.*, b.city as buyer_city FROM
table_1 a
JOIN table_2 b ON (a.buyer_id = b.person_id)
然后我可以再做第二次查询以获得卖家。但有没有办法在单个查询中获得我想要的东西?
答案 0 :(得分:2)
加入SELECT a.*,
buyer.city as buyer_city,
seller.city as seller_city
FROM table_1 a
JOIN table_2 buyer ON a.buyer_id = buyer.person_id
JOIN table_2 seller ON a.seller_id = seller.person_id
两次
>> A = zeros(5)
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0