Search code examples
mysqldatabasejoinrelational

mysql - query three tables


I have a relational database with three tables. The first containts id's that relate to the second. The second contains id's that relate to the third. The third contains the results I am after.

Is it possible with a single query to query an id in the first table which gives all results from the third table that relate to it?

Sorry I am new to mySQL.


Solution

  • Mysql Join

    Try this

    select * from table1 t1
    join table2 t2 on t1.t2ref = t2.id
    join table3 t3 on t2.t3ref = t3.id
    

    Add a where clause to search for certain rows in table1

    where t1.field = 'value'