MySQL/Pivot table for two tables





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I was trying to get multiple rows into a single row with dynamic columns created as per the data. I have two tables combined with foreign key.



Table 1:



| id | name | Invoice value | invoice_date |
|----|------|---------------|--------------|
| 1 | A | 5000 | 30-01-2016 |
| 2 | B | 8000 | 02-05-2016 |
| 3 | C | 10000 | 03-05-2016 |


Table 2:



| id | invoice_id | duedate    | amount | percentage |
|----|------------|------------|--------|------------|
| 1 | 1 | 15-01-2016 | 2500 | 50% |
| 2 | 1 | 30-01-2016 | 2500 | 50% |
| 3 | 2 | 15-02-2016 | 8000 | 100% |
| 4 | 3 | 15-05-2016 | 5000 | 50% |
| 5 | 3 | 19-05-2016 | 2500 | 25% |
| 6 | 3 | 25-05-2016 | 2500 | 25% |


Desired output:



| name | invoice_value | invoice_date | due date1  | due amount1 | due date2  | due amount2 | due date3  | due amount3 |
|------|---------------|--------------|------------|-------------|------------|-------------|------------|-------------|
| A | 5000 | 30-01-2016 | 15-01-2016 | 2500 | 30-01-2016 | 04-11-1906 | null | null |
| B | 8000 | 02-05-2016 | 15-02-2016 | 8000 | null | null | null | null |
| C | 10000 | 03-05-2016 | 15-05-2016 | 5000 | 19-05-2016 | 2500 | 19-05-2016 | 2500 |


When I tried have used group-concat for the multiple columns it's giving results with comma separated. But I want as desired output. Please somebody help to solve this issue how to write a query for this.



I was using the following query but it's giving results as comma separated result:



SELECT  T1.name,T1.invoice_value,T1.invoice_date,T1.duedate,T1.dueamount
FROM
( SELECT table1.name , table1.invoice_value, table1.invoice_date,
group_concat(table2.duedate1) as duedate,
group_concat(table2.dueamount1) as dueamount
FROM table1
LEFT JOIN table2 ON table1.id=table2.invoice_id
)T1
Group By T1.id









share|improve this question
















bumped to the homepage by Community 23 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1





    Have a look at how to build a dynamic PIVOT query using MySql: stackoverflow.com/a/12599372/3270427

    – McNets
    Jun 5 '17 at 15:41


















1















I was trying to get multiple rows into a single row with dynamic columns created as per the data. I have two tables combined with foreign key.



Table 1:



| id | name | Invoice value | invoice_date |
|----|------|---------------|--------------|
| 1 | A | 5000 | 30-01-2016 |
| 2 | B | 8000 | 02-05-2016 |
| 3 | C | 10000 | 03-05-2016 |


Table 2:



| id | invoice_id | duedate    | amount | percentage |
|----|------------|------------|--------|------------|
| 1 | 1 | 15-01-2016 | 2500 | 50% |
| 2 | 1 | 30-01-2016 | 2500 | 50% |
| 3 | 2 | 15-02-2016 | 8000 | 100% |
| 4 | 3 | 15-05-2016 | 5000 | 50% |
| 5 | 3 | 19-05-2016 | 2500 | 25% |
| 6 | 3 | 25-05-2016 | 2500 | 25% |


Desired output:



| name | invoice_value | invoice_date | due date1  | due amount1 | due date2  | due amount2 | due date3  | due amount3 |
|------|---------------|--------------|------------|-------------|------------|-------------|------------|-------------|
| A | 5000 | 30-01-2016 | 15-01-2016 | 2500 | 30-01-2016 | 04-11-1906 | null | null |
| B | 8000 | 02-05-2016 | 15-02-2016 | 8000 | null | null | null | null |
| C | 10000 | 03-05-2016 | 15-05-2016 | 5000 | 19-05-2016 | 2500 | 19-05-2016 | 2500 |


When I tried have used group-concat for the multiple columns it's giving results with comma separated. But I want as desired output. Please somebody help to solve this issue how to write a query for this.



I was using the following query but it's giving results as comma separated result:



SELECT  T1.name,T1.invoice_value,T1.invoice_date,T1.duedate,T1.dueamount
FROM
( SELECT table1.name , table1.invoice_value, table1.invoice_date,
group_concat(table2.duedate1) as duedate,
group_concat(table2.dueamount1) as dueamount
FROM table1
LEFT JOIN table2 ON table1.id=table2.invoice_id
)T1
Group By T1.id









share|improve this question
















bumped to the homepage by Community 23 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1





    Have a look at how to build a dynamic PIVOT query using MySql: stackoverflow.com/a/12599372/3270427

    – McNets
    Jun 5 '17 at 15:41














1












1








1








I was trying to get multiple rows into a single row with dynamic columns created as per the data. I have two tables combined with foreign key.



Table 1:



| id | name | Invoice value | invoice_date |
|----|------|---------------|--------------|
| 1 | A | 5000 | 30-01-2016 |
| 2 | B | 8000 | 02-05-2016 |
| 3 | C | 10000 | 03-05-2016 |


Table 2:



| id | invoice_id | duedate    | amount | percentage |
|----|------------|------------|--------|------------|
| 1 | 1 | 15-01-2016 | 2500 | 50% |
| 2 | 1 | 30-01-2016 | 2500 | 50% |
| 3 | 2 | 15-02-2016 | 8000 | 100% |
| 4 | 3 | 15-05-2016 | 5000 | 50% |
| 5 | 3 | 19-05-2016 | 2500 | 25% |
| 6 | 3 | 25-05-2016 | 2500 | 25% |


Desired output:



| name | invoice_value | invoice_date | due date1  | due amount1 | due date2  | due amount2 | due date3  | due amount3 |
|------|---------------|--------------|------------|-------------|------------|-------------|------------|-------------|
| A | 5000 | 30-01-2016 | 15-01-2016 | 2500 | 30-01-2016 | 04-11-1906 | null | null |
| B | 8000 | 02-05-2016 | 15-02-2016 | 8000 | null | null | null | null |
| C | 10000 | 03-05-2016 | 15-05-2016 | 5000 | 19-05-2016 | 2500 | 19-05-2016 | 2500 |


When I tried have used group-concat for the multiple columns it's giving results with comma separated. But I want as desired output. Please somebody help to solve this issue how to write a query for this.



I was using the following query but it's giving results as comma separated result:



SELECT  T1.name,T1.invoice_value,T1.invoice_date,T1.duedate,T1.dueamount
FROM
( SELECT table1.name , table1.invoice_value, table1.invoice_date,
group_concat(table2.duedate1) as duedate,
group_concat(table2.dueamount1) as dueamount
FROM table1
LEFT JOIN table2 ON table1.id=table2.invoice_id
)T1
Group By T1.id









share|improve this question
















I was trying to get multiple rows into a single row with dynamic columns created as per the data. I have two tables combined with foreign key.



Table 1:



| id | name | Invoice value | invoice_date |
|----|------|---------------|--------------|
| 1 | A | 5000 | 30-01-2016 |
| 2 | B | 8000 | 02-05-2016 |
| 3 | C | 10000 | 03-05-2016 |


Table 2:



| id | invoice_id | duedate    | amount | percentage |
|----|------------|------------|--------|------------|
| 1 | 1 | 15-01-2016 | 2500 | 50% |
| 2 | 1 | 30-01-2016 | 2500 | 50% |
| 3 | 2 | 15-02-2016 | 8000 | 100% |
| 4 | 3 | 15-05-2016 | 5000 | 50% |
| 5 | 3 | 19-05-2016 | 2500 | 25% |
| 6 | 3 | 25-05-2016 | 2500 | 25% |


Desired output:



| name | invoice_value | invoice_date | due date1  | due amount1 | due date2  | due amount2 | due date3  | due amount3 |
|------|---------------|--------------|------------|-------------|------------|-------------|------------|-------------|
| A | 5000 | 30-01-2016 | 15-01-2016 | 2500 | 30-01-2016 | 04-11-1906 | null | null |
| B | 8000 | 02-05-2016 | 15-02-2016 | 8000 | null | null | null | null |
| C | 10000 | 03-05-2016 | 15-05-2016 | 5000 | 19-05-2016 | 2500 | 19-05-2016 | 2500 |


When I tried have used group-concat for the multiple columns it's giving results with comma separated. But I want as desired output. Please somebody help to solve this issue how to write a query for this.



I was using the following query but it's giving results as comma separated result:



SELECT  T1.name,T1.invoice_value,T1.invoice_date,T1.duedate,T1.dueamount
FROM
( SELECT table1.name , table1.invoice_value, table1.invoice_date,
group_concat(table2.duedate1) as duedate,
group_concat(table2.dueamount1) as dueamount
FROM table1
LEFT JOIN table2 ON table1.id=table2.invoice_id
)T1
Group By T1.id






mysql php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 27 '18 at 21:46









freginold

1093




1093










asked May 30 '16 at 11:53









0601199106011991

1063




1063





bumped to the homepage by Community 23 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 23 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1





    Have a look at how to build a dynamic PIVOT query using MySql: stackoverflow.com/a/12599372/3270427

    – McNets
    Jun 5 '17 at 15:41














  • 1





    Have a look at how to build a dynamic PIVOT query using MySql: stackoverflow.com/a/12599372/3270427

    – McNets
    Jun 5 '17 at 15:41








1




1





Have a look at how to build a dynamic PIVOT query using MySql: stackoverflow.com/a/12599372/3270427

– McNets
Jun 5 '17 at 15:41





Have a look at how to build a dynamic PIVOT query using MySql: stackoverflow.com/a/12599372/3270427

– McNets
Jun 5 '17 at 15:41










1 Answer
1






active

oldest

votes


















0














(Technically, this is 'not an answer', but claim it needs to be said.)



There will be an arbitrary number of columns, correct? Then (1) the output is impractical, and (2) it would be virtually impossible to generate the output in SQL.



So, (1) rethink the requirement, then (2) consider whether to do it in PHP instead.






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2fdba.stackexchange.com%2fquestions%2f139908%2fmysql-pivot-table-for-two-tables%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









    0














    (Technically, this is 'not an answer', but claim it needs to be said.)



    There will be an arbitrary number of columns, correct? Then (1) the output is impractical, and (2) it would be virtually impossible to generate the output in SQL.



    So, (1) rethink the requirement, then (2) consider whether to do it in PHP instead.






    share|improve this answer




























      0














      (Technically, this is 'not an answer', but claim it needs to be said.)



      There will be an arbitrary number of columns, correct? Then (1) the output is impractical, and (2) it would be virtually impossible to generate the output in SQL.



      So, (1) rethink the requirement, then (2) consider whether to do it in PHP instead.






      share|improve this answer


























        0












        0








        0







        (Technically, this is 'not an answer', but claim it needs to be said.)



        There will be an arbitrary number of columns, correct? Then (1) the output is impractical, and (2) it would be virtually impossible to generate the output in SQL.



        So, (1) rethink the requirement, then (2) consider whether to do it in PHP instead.






        share|improve this answer













        (Technically, this is 'not an answer', but claim it needs to be said.)



        There will be an arbitrary number of columns, correct? Then (1) the output is impractical, and (2) it would be virtually impossible to generate the output in SQL.



        So, (1) rethink the requirement, then (2) consider whether to do it in PHP instead.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 6 '16 at 5:35









        Rick JamesRick James

        43.9k22361




        43.9k22361






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • 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%2fdba.stackexchange.com%2fquestions%2f139908%2fmysql-pivot-table-for-two-tables%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

            Liste der Baudenkmale in Friedland (Mecklenburg)

            Single-Malt-Whisky

            Czorneboh