0

我有两张桌子:

Subscriber_detail

SubscriberID | Merchant_ID 
23           |  24

User_Detail

User_ID | User_RegisteredID

23      | 1001001
24      | 1001002

需要User_RegisteredIDUser_Login表中获取SubscriberID and Merchant_ID from Subscriber_Detail

4

1 回答 1

1
SELECT  sub.User_RegisteredID AS SubscriberRegisteredID,
        mer.User_RegisteredID AS MerchantRegisteredID
FROM    Subscriber_detail s
        JOIN User_Detail sub on s.SubscriberID = sub.User_ID
        JOIN User_Detail mer ON s.Merchant_ID = mer.User_ID

您需要连接两个表,并且表User_Detail应该连接两次,因为表中有两列Subscriber_detail被引用。

如果一列是NULLABLELEFT JOIN则应改为使用。

SELECT  sub.User_RegisteredID AS SubscriberRegisteredID,
        mer.User_RegisteredID AS MerchantRegisteredID
FROM    Subscriber_detail s
        LEFT JOIN User_Detail sub on s.SubscriberID = sub.User_ID
        LEFT JOIN User_Detail mer ON s.Merchant_ID = mer.User_ID
于 2013-05-03T05:08:17.187 回答