0

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 ....

3
  • Look up JOINs. Commented Dec 28, 2018 at 19:41
  • 1
    It's not clear what are you expect. Please, provide some example of expected output ? Commented Dec 28, 2018 at 19:47
  • 1
    how can I merge all 4 tables based on ids? Commented Dec 28, 2018 at 19:50

1 Answer 1

2
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
Sign up to request clarification or add additional context in comments.

1 Comment

or if you don't know the id you can write where t4.machine = 'mercedes'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.