在MySQL中,我想SELECT A.* FROM A
其中內加入條件滿足(B
加入表)或通過另一種是直接連接表(C
),WHERE B.field = myvalue
。任何人都可以指出獲得結果的正確方法嗎?Mysql的內連接兩次,使用WHERE上兩個連接
我有以下表格:A,B,C,這是如下關聯(A加入B,B連接C,A加入C):
B
/ \
A --- C
它看起來很簡單,但我得到當我運行下面的代碼,即使我得到的結果,當我將搜索限制在一個空集,只是到C加入B:
SELECT A.* FROM A
INNER JOIN C ON C.id = A.c_id
INNER JOIN B AS B_thru_C ON B_thru_C.id = C.b_id
INNER JOIN B AS B_from_A ON B_from_A.id = A.b_id
WHERE B_thru_C.field = 'myvalue' OR B_from_A.field = 'myvalue';
# yields an empty set
SELECT A.* FROM A
INNER JOIN C ON C.id = A.c_id
INNER JOIN B AS B_thru_C ON B_thru_C.id = C.b_id
WHERE B_thru_C.field = 'myvalue';
# yields results
即使沒有成功連接到B或C,查詢是否應該從集合A返回記錄?還是應該在這種情況下消除它們? – 2013-04-05 04:12:13