如何查询其id值存在于同一个表B中的2 id的字段

时间:2014-03-31 10:47:44

标签: sql sqlite

**Friends Table**     
-------------------------
id    seller_id    buyer_id
-------------------------
1     101           102
2     102           104
3     103           101
4     104           101

**Name Table**     
--------------
id    name_id
--------------
101   Robin
102   Goblin
103   Ork
104   Wizard

我只想让Friends表显示Name而不是name_id和friend_id。 我知道这很简单,但我找不到合适的词来为它辩护。请帮忙..

4 个答案:

答案 0 :(得分:1)

select f.id,n.name_id as "seller_id",nb.name_id  as "buyer_id"
from [Friends Table] f
join [Name Table] n on n.seller_id=f.id
join [Name Table] nb on nb.buyer_id=f.id

答案 1 :(得分:1)

这可能会对你有帮助。

SELECT F.friend_id, F.id N.name_id FROM Friends_Table F INNER JOIN Name_Table N ON F.id= N.id

答案 2 :(得分:1)

您可以执行两次JOIN来实现这一目标:

SELECT 
    f.id
    , n1.name_id As seller
    , n2.name_id As buyer
FROM Friends f
    INNER JOIN Name n1 ON n1.id = f.seller_id
    INNER JOIN Name n2 ON n2.id = f.buyer_id

答案 3 :(得分:0)

也许这就是你要找的东西?

select n.name_id
from Friends as f
join Name as n
on f.id = n.id
group by n.name_id