我有两张桌子:
Subscriber_detail
SubscriberID | Merchant_ID
23 | 24
User_Detail
User_ID | User_RegisteredID
23 | 1001001
24 | 1001002
需要User_RegisteredID
从User_Login
表中获取SubscriberID and Merchant_ID from Subscriber_Detail
我有两张桌子:
Subscriber_detail
SubscriberID | Merchant_ID
23 | 24
User_Detail
User_ID | User_RegisteredID
23 | 1001001
24 | 1001002
需要User_RegisteredID
从User_Login
表中获取SubscriberID and Merchant_ID from Subscriber_Detail
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
被引用。
如果一列是NULLABLE,LEFT 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