1

在 MySQL 中,我想直接(连接表)通过另一个连接表()SELECT A.* FROM A满足内部连接条件。谁能指出获得结果的正确方法?BCWHERE 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
4

1 回答 1

0

这个怎么样?

SELECT A.* FROM A
LEFT OUTER JOIN C ON C.id = A.c_id
INNER JOIN B ON B.id = A.b_id OR B.id = C.b_id
WHERE B.field = 'myvalue';
于 2013-04-05T04:08:02.987 回答