CoderCoder42
CoderCoder42

Reputation: 171

Multiple table inner join in mysql query

I have a link table linking IDs to 4 different tables:

table 1

Id  name
----------
1   eduard
2   remus
3   gabi

table 2

Id  ocupation
-----------------
1   manager
2   office worker

table 3

Id  sex
----------
1   male
2   female

table 4

Id  machine
------------
1   audi
2   mercedes
3   renault

assoc_table

Id   Id_table1  Id_table2 Id_table3  Id_table4
----------------------------------------------
1    1          1         1           1
2    2          1         1           3
3    3          2         1           3

I want to link these 4 tables together so I can look for example:

All colleagues driving the Mercedes car ....

Upvotes: 0

Views: 44

Answers (1)

Zia Ur Rehman
Zia Ur Rehman

Reputation: 116

SELECT 
    t1.name, t2.ocupation, t3.sex, t4.machine
FROM
    assoc_table ast
        INNER JOIN
    table1 t1 ON t1.Id = ast.Id_table1
        INNER JOIN
    table2 t2 ON t2.Id = ast.Id_table2
        INNER JOIN
    table3 t3 ON t3.Id = ast.Id_table3
        INNER JOIN
    table4 t4 ON t4.Id = ast.Id_table4
WHERE
    t4.Id = 2

Upvotes: 2

Related Questions