在查询中帮助JOIN

时间:2011-05-21 21:23:56

标签: mysql sql join

SELECT * FROM
Table_A
LEFT JOIN Table_B
 ON (Table_A.A_ID = Table_B.A_ID)
INNER JOIN Table_C ON (Table_C.C_ID = Table_B.C_ID)
WHERE Table_A.ID = 3

目前返回0行。

如何设置它以便我总是得到Table_A,即使Table_B或Table_C没有行也是如此。我仍然希望保持Table_B和Table_C之间的INNER JOIN,这样我就永远不会得到没有C的B。

2 个答案:

答案 0 :(得分:2)

查询表B和表C.例如

SELECT * 
FROM Table_A 
LEFT JOIN 
    (SELECT * 
         FROM Table_B
         INNER JOIN Table_C ON (Table_C.C_ID = Table_B.C_ID)
    ) B_AND_C ON (Table_A.A_ID = B_AND_C.A_ID)
WHERE Table_A.ID = 3

答案 1 :(得分:2)

您可能不必使用内嵌视图。我现在不在MySQL的机器前,所以我无法检查,但你可以尝试

SELECT *
FROM table_A a 
LEFT JOIN (table_B b 
    INNER JOIN table_C c
    ON b.c_id = c.c_id)
ON a.b_id = b.b_id
WHERE 
    a.a_id =3