Search code examples
mysqlsqlinner-join

join a to c where table a and table c has only table b as relation


I have 3 tables; A,B,C

A is in relation with B and B is in relation with C

I want to use select query on A and take a column of C in the query

Using inner join, I can only reach B but I want to reach C which has no direct relation with A


Solution

  • In this case you need to use 2 inner joins to get data from tablec like following.

    select a.*,c.coltoselect
    from tablea a
    inner join tableb b on a.abcommoncolumn=b.abcommoncolumn
    inner join tablec c on b.bccommoncolumn=c.bccommoncolumn