在 MySQL 中,我想直接(连接表)或通过另一个连接表()SELECT A.* FROM A
满足内部连接条件。谁能指出获得结果的正确方法?B
C
WHERE B.field = myvalue
我有以下表格:A、B、C,关联如下(A 连接 B,B 连接 C,A 连接 C):
B
/ \
A --- C
它看起来很简单,但是当我运行以下代码时我得到一个空集,即使当我将搜索限制为仅加入 B 到 C 时我得到了结果:
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