[英]What type of join should I use?
我的加盟知識很糟糕,為此我找不到正確的知識。 我想加入TableA.userID和TableB.otherID上的兩個表以匹配所需的輸出。 我桌上沒有任何外鍵或主鍵。 我應該在這里使用什么?
TableA:
+--------+--------+---------+
| userID | field1 | field2 |
+--------+--------+---------+
TableB:
+--------+----------+---------+---------+---------+
| userID | otherID | myData1 | myData2 | myData3 |
+--------+----------+---------+---------+---------+
Desired Output:
+--------+----------+---------+---------+---------+--------+--------+
| userID | otherID | myData1 | myData2 | myData3 | field1 | field2 |
+--------+----------+---------+---------+---------+--------+--------+
應該這樣做:
select a.userID, b.otherID, b.myData1, b.myData2, b.myData3, a.field1, a.field1
from TableA a
inner join TableB b
on a.userID = b.otherID
我建議在TableA.userID和TableB.otherID上添加索引。
也許我誤解了這個問題,但是假設USERID和OTHERID都是相同的數據類型,並且您不需要執行任何數據轉換,您是否不能僅使用簡單的INNER JOIN。
SELECT A.userID, B.otherID, B.myData, B.myData2, B.myData3, A.field1, A.field2
FROM TableA as A
INNER JOIN TableB as B
ON A.UserId = B.OtherId
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.