dongxifu5009 2011-06-23 07:19
浏览 23
已采纳

左边将1个表连接到MYSQL中的多个表

I have 4 tables, which are linked together with a foreign key from another, eg Table 2 has fk_table1, Table 3 has fk_table2, Table 4 has fk_table3.

The first 3 tables in this chain all have corresponding data for each entry. However, Table 4 contain optional data, therefore, there may not be a corresponding entry for a field in say Table 3.

But I want those data from Table 4 too. This is currently what I have, but doesn't work.

SELECT *
        FROM T1, T2, T3 LEFT JOIN T4
        WHERE T1.t1 = T2.t1
        AND T2.t2 = T3.t2
        AND T3.t3 = T4.t3
  • 写回答

3条回答 默认 最新

  • dongyi1777 2011-06-23 07:27
    关注

    If only T4 is optional, use LEFT JOIN only on that table:

    SELECT *
    FROM T1
    JOIN T2 ON T1.t1 = T2.t1
    JOIN T3 ON T2.t2 = T3.t2
    LEFT JOIN T4 ON T3.t3 = T4.t3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?