2013-03-06 72 views
0

我的加入知識是可怕的,我找不到這個正確的。我想加入TableA.userID和TableB.otherID中的兩個表以匹配所需的輸出。我沒有在表上設置任何外鍵或主鍵。我應該在這裏使用什麼?我應該使用什麼類型的連接?

TableA: 
+--------+--------+---------+ 
| userID | field1 | field2 | 
+--------+--------+---------+ 

TableB: 
+--------+----------+---------+---------+---------+ 
| userID | otherID | myData1 | myData2 | myData3 | 
+--------+----------+---------+---------+---------+ 

Desired Output: 
+--------+----------+---------+---------+---------+--------+--------+ 
| userID | otherID | myData1 | myData2 | myData3 | field1 | field2 | 
+--------+----------+---------+---------+---------+--------+--------+ 
+0

這是一個NULL的問題。你有任何NULL用戶ID?如果不使用內部連接 – 2013-03-06 23:23:52

回答

2

這應做到:

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指標。

1

也許我誤解了這個問題,但是不能僅僅使用簡單的INNER JOIN,假設USERID和OTHERID都是相同的數據類型,並且不需要執行任何數據轉換。

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 
相關問題