Mysql multiple left join on same table for grouping results











up vote
-3
down vote

favorite












Table
This stable stores some history of address changes



Id  Name    Address        Group Id
1 AAA Primary 1
2 BBB Secondary 1
3 CCC Primary 1
4 DDD Secondary 1
5 EEE Primary 1
6 FFF Primary 2
7 GGG Secondary 2
8 HHH Primary 3
9 III Secondary 4
10 JJJ Secondary 1


Result I need a result like beleow



Primary Address Secondary Address
AAA BBB
CCC DDD
EEE JJJ
FFF GGG
HHH NULL
NULL III


Is it possible to achieve this result with mysql joins










share|improve this question
























  • Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
    – Raymond Nijland
    Nov 19 at 12:36










  • Will the row of a secondary address be always after the Primary address ?
    – Madhur Bhaiya
    Nov 20 at 13:09















up vote
-3
down vote

favorite












Table
This stable stores some history of address changes



Id  Name    Address        Group Id
1 AAA Primary 1
2 BBB Secondary 1
3 CCC Primary 1
4 DDD Secondary 1
5 EEE Primary 1
6 FFF Primary 2
7 GGG Secondary 2
8 HHH Primary 3
9 III Secondary 4
10 JJJ Secondary 1


Result I need a result like beleow



Primary Address Secondary Address
AAA BBB
CCC DDD
EEE JJJ
FFF GGG
HHH NULL
NULL III


Is it possible to achieve this result with mysql joins










share|improve this question
























  • Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
    – Raymond Nijland
    Nov 19 at 12:36










  • Will the row of a secondary address be always after the Primary address ?
    – Madhur Bhaiya
    Nov 20 at 13:09













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











Table
This stable stores some history of address changes



Id  Name    Address        Group Id
1 AAA Primary 1
2 BBB Secondary 1
3 CCC Primary 1
4 DDD Secondary 1
5 EEE Primary 1
6 FFF Primary 2
7 GGG Secondary 2
8 HHH Primary 3
9 III Secondary 4
10 JJJ Secondary 1


Result I need a result like beleow



Primary Address Secondary Address
AAA BBB
CCC DDD
EEE JJJ
FFF GGG
HHH NULL
NULL III


Is it possible to achieve this result with mysql joins










share|improve this question















Table
This stable stores some history of address changes



Id  Name    Address        Group Id
1 AAA Primary 1
2 BBB Secondary 1
3 CCC Primary 1
4 DDD Secondary 1
5 EEE Primary 1
6 FFF Primary 2
7 GGG Secondary 2
8 HHH Primary 3
9 III Secondary 4
10 JJJ Secondary 1


Result I need a result like beleow



Primary Address Secondary Address
AAA BBB
CCC DDD
EEE JJJ
FFF GGG
HHH NULL
NULL III


Is it possible to achieve this result with mysql joins







mysql left-join






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 13:50

























asked Nov 19 at 12:31









Alex

11




11












  • Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
    – Raymond Nijland
    Nov 19 at 12:36










  • Will the row of a secondary address be always after the Primary address ?
    – Madhur Bhaiya
    Nov 20 at 13:09


















  • Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
    – Raymond Nijland
    Nov 19 at 12:36










  • Will the row of a secondary address be always after the Primary address ?
    – Madhur Bhaiya
    Nov 20 at 13:09
















Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
– Raymond Nijland
Nov 19 at 12:36




Normally you can pivot only problem is you don't same to have a column within a group where you can indentify the correct position so the answer is No
– Raymond Nijland
Nov 19 at 12:36












Will the row of a secondary address be always after the Primary address ?
– Madhur Bhaiya
Nov 20 at 13:09




Will the row of a secondary address be always after the Primary address ?
– Madhur Bhaiya
Nov 20 at 13:09












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You can try using case when expression



select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename





share|improve this answer





















  • I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
    – Alex
    Nov 19 at 13:46











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',
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%2f53374723%2fmysql-multiple-left-join-on-same-table-for-grouping-results%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








up vote
0
down vote













You can try using case when expression



select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename





share|improve this answer





















  • I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
    – Alex
    Nov 19 at 13:46















up vote
0
down vote













You can try using case when expression



select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename





share|improve this answer





















  • I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
    – Alex
    Nov 19 at 13:46













up vote
0
down vote










up vote
0
down vote









You can try using case when expression



select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename





share|improve this answer












You can try using case when expression



select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 12:37









fa06

9,7231917




9,7231917












  • I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
    – Alex
    Nov 19 at 13:46


















  • I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
    – Alex
    Nov 19 at 13:46
















I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
– Alex
Nov 19 at 13:46




I have tried the same but it doesn't give me primary and secondary in the same row. Either primary or secondary is coming null with this case option.
– Alex
Nov 19 at 13:46


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53374723%2fmysql-multiple-left-join-on-same-table-for-grouping-results%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

Ottavio Pratesi

Tricia Helfer

15 giugno