Multiple table inner join in mysql query












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










share|improve this question

























  • Look up JOINs.

    – sticky bit
    Dec 28 '18 at 19:41






  • 1





    It's not clear what are you expect. Please, provide some example of expected output ?

    – MorganFreeFarm
    Dec 28 '18 at 19:47






  • 1





    how can I merge all 4 tables based on ids?

    – CoderCoder42
    Dec 28 '18 at 19:50
















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










share|improve this question

























  • Look up JOINs.

    – sticky bit
    Dec 28 '18 at 19:41






  • 1





    It's not clear what are you expect. Please, provide some example of expected output ?

    – MorganFreeFarm
    Dec 28 '18 at 19:47






  • 1





    how can I merge all 4 tables based on ids?

    – CoderCoder42
    Dec 28 '18 at 19:50














0












0








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










share|improve this question
















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







mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 7:57









Barbaros Özhan

12.6k71532




12.6k71532










asked Dec 28 '18 at 19:32









CoderCoder42CoderCoder42

33




33













  • Look up JOINs.

    – sticky bit
    Dec 28 '18 at 19:41






  • 1





    It's not clear what are you expect. Please, provide some example of expected output ?

    – MorganFreeFarm
    Dec 28 '18 at 19:47






  • 1





    how can I merge all 4 tables based on ids?

    – CoderCoder42
    Dec 28 '18 at 19:50



















  • Look up JOINs.

    – sticky bit
    Dec 28 '18 at 19:41






  • 1





    It's not clear what are you expect. Please, provide some example of expected output ?

    – MorganFreeFarm
    Dec 28 '18 at 19:47






  • 1





    how can I merge all 4 tables based on ids?

    – CoderCoder42
    Dec 28 '18 at 19:50

















Look up JOINs.

– sticky bit
Dec 28 '18 at 19:41





Look up JOINs.

– sticky bit
Dec 28 '18 at 19:41




1




1





It's not clear what are you expect. Please, provide some example of expected output ?

– MorganFreeFarm
Dec 28 '18 at 19:47





It's not clear what are you expect. Please, provide some example of expected output ?

– MorganFreeFarm
Dec 28 '18 at 19:47




1




1





how can I merge all 4 tables based on ids?

– CoderCoder42
Dec 28 '18 at 19:50





how can I merge all 4 tables based on ids?

– CoderCoder42
Dec 28 '18 at 19:50












1 Answer
1






active

oldest

votes


















2














SELECT 
t1.name, t2.name, t3.name, 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





share|improve this answer
























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

    – Zia Ur Rehman
    Dec 28 '18 at 20:01











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963466%2fmultiple-table-inner-join-in-mysql-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














SELECT 
t1.name, t2.name, t3.name, 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





share|improve this answer
























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

    – Zia Ur Rehman
    Dec 28 '18 at 20:01
















2














SELECT 
t1.name, t2.name, t3.name, 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





share|improve this answer
























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

    – Zia Ur Rehman
    Dec 28 '18 at 20:01














2












2








2







SELECT 
t1.name, t2.name, t3.name, 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





share|improve this answer













SELECT 
t1.name, t2.name, t3.name, 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






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 28 '18 at 19:54









Zia Ur RehmanZia Ur Rehman

1165




1165













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

    – Zia Ur Rehman
    Dec 28 '18 at 20:01



















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

    – Zia Ur Rehman
    Dec 28 '18 at 20:01

















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

– Zia Ur Rehman
Dec 28 '18 at 20:01





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

– Zia Ur Rehman
Dec 28 '18 at 20:01


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963466%2fmultiple-table-inner-join-in-mysql-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'