2017-09-22 127 views
1

我有兩個表,表1和表2,表1有member_id,名字和姓氏, 表2有buyer_id和seller_id我想選擇名字和姓氏買方和賣方在一排選擇不同條件的列兩次

Table_1 
    member id | firstname | lastname 
    1  | John  | Richard 
    2  | Joseph | Curly 
    3  | Ismail | Jake 
    4  | Elijah | Michael 

    Table_2 
    seller_id | buyer_id| 
    2  |  1 | 
    3  |  4 | 

我一直在試圖與行返回一個表像這樣

seller_id | buyer_id | firstname | lastname | firstname | lastname| 
    2  | 1  | Joseph | Curly | John  | Richard | 

查詢我使用的是

SELECT a.firstname,a.lastname,a.firstname,a.lastname,b.seller_id,b.buyer_id 
     FROM Table_1 as a,Table_2 as b WHERE b.buyer_id = a.member_id 

回答

1

你需要加入table_1兩次(一個用於連接seller_id,一個用於連接buyer_id

SELECT 
    a.firstname 
    ,a.lastname 
    ,c.firstname 
    ,c.lastname 
    ,b.seller_id 
    ,b.buyer_id 
    FROM Table_2 as b 
    INNER JOIN Table_1 as a ON a.member_id = b.seller_id 
    INNER JOIN Table_1 as c On c.member_id = b.buyer_id 
+0

感謝lot.I會嘗試這個馬上我真的不明白表聯接 – Muhammed

+0

你需要一個解釋,或者你有打開答案? – scaisEdge

+2

對於皮特的緣故。已經有6分鐘的時間了,你正在尋求接受嗎?給這個可憐的人一個閱讀和理解答案的機會。 –