I want to display a summary like this based on the three tables









up vote
1
down vote

favorite












My 3 MYSQL tables are as follows:



Table 1: citizen



=============================
ID | Name | Sex | Address |
=============================
5 | James | Male | India
6 | Shella|Female | India
7 | Jan | Male | NY
8 | May | Female | USA
==============================


Table 2: benefits



========================== 
ID| benefits
==========================
1 | SSS
2 | Coco Life
3 | PhiHealth
4 | Sunlife
==========================


Table 3: pensioners



============================
ID| benefits_ID | citizen_ID
============================
1 | 1 | 5
2 | 2 | 6
3 | 1 | 7
4 | 4 | 7
==========================


I want to display that looks like this:



====================================================================
Address | Total Citizen | Male | Female | SSS | Coco Life | Others |
====================================================================
India | 2 | 1 | 1 | 1 | 1 | 0 |
NY | 1 | 1 | 0 | 1 | 0 | 1 |
USA | 1 | 0 | 1 | 0 | 0 | 0 |
==================================================================


Anybody can give me a hint on how to do this?










share|improve this question

























    up vote
    1
    down vote

    favorite












    My 3 MYSQL tables are as follows:



    Table 1: citizen



    =============================
    ID | Name | Sex | Address |
    =============================
    5 | James | Male | India
    6 | Shella|Female | India
    7 | Jan | Male | NY
    8 | May | Female | USA
    ==============================


    Table 2: benefits



    ========================== 
    ID| benefits
    ==========================
    1 | SSS
    2 | Coco Life
    3 | PhiHealth
    4 | Sunlife
    ==========================


    Table 3: pensioners



    ============================
    ID| benefits_ID | citizen_ID
    ============================
    1 | 1 | 5
    2 | 2 | 6
    3 | 1 | 7
    4 | 4 | 7
    ==========================


    I want to display that looks like this:



    ====================================================================
    Address | Total Citizen | Male | Female | SSS | Coco Life | Others |
    ====================================================================
    India | 2 | 1 | 1 | 1 | 1 | 0 |
    NY | 1 | 1 | 0 | 1 | 0 | 1 |
    USA | 1 | 0 | 1 | 0 | 0 | 0 |
    ==================================================================


    Anybody can give me a hint on how to do this?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      My 3 MYSQL tables are as follows:



      Table 1: citizen



      =============================
      ID | Name | Sex | Address |
      =============================
      5 | James | Male | India
      6 | Shella|Female | India
      7 | Jan | Male | NY
      8 | May | Female | USA
      ==============================


      Table 2: benefits



      ========================== 
      ID| benefits
      ==========================
      1 | SSS
      2 | Coco Life
      3 | PhiHealth
      4 | Sunlife
      ==========================


      Table 3: pensioners



      ============================
      ID| benefits_ID | citizen_ID
      ============================
      1 | 1 | 5
      2 | 2 | 6
      3 | 1 | 7
      4 | 4 | 7
      ==========================


      I want to display that looks like this:



      ====================================================================
      Address | Total Citizen | Male | Female | SSS | Coco Life | Others |
      ====================================================================
      India | 2 | 1 | 1 | 1 | 1 | 0 |
      NY | 1 | 1 | 0 | 1 | 0 | 1 |
      USA | 1 | 0 | 1 | 0 | 0 | 0 |
      ==================================================================


      Anybody can give me a hint on how to do this?










      share|improve this question













      My 3 MYSQL tables are as follows:



      Table 1: citizen



      =============================
      ID | Name | Sex | Address |
      =============================
      5 | James | Male | India
      6 | Shella|Female | India
      7 | Jan | Male | NY
      8 | May | Female | USA
      ==============================


      Table 2: benefits



      ========================== 
      ID| benefits
      ==========================
      1 | SSS
      2 | Coco Life
      3 | PhiHealth
      4 | Sunlife
      ==========================


      Table 3: pensioners



      ============================
      ID| benefits_ID | citizen_ID
      ============================
      1 | 1 | 5
      2 | 2 | 6
      3 | 1 | 7
      4 | 4 | 7
      ==========================


      I want to display that looks like this:



      ====================================================================
      Address | Total Citizen | Male | Female | SSS | Coco Life | Others |
      ====================================================================
      India | 2 | 1 | 1 | 1 | 1 | 0 |
      NY | 1 | 1 | 0 | 1 | 0 | 1 |
      USA | 1 | 0 | 1 | 0 | 0 | 0 |
      ==================================================================


      Anybody can give me a hint on how to do this?







      php mysql sql mysqli






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 11:44









      Kinanda Mata

      186




      186






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can do a Left Join from the Address table to the benefits table, via pensioners table, using the appropriate relationships. Left join will allows us to consider a Address even when there is no corresponding benefits entry for any of its citizens.



          In order to count total citizens, male count and female count, you now need to use COUNT(DISTINCT ID) after the join. As Joining may create duplicate rows, as a citizen may have more than one benefits.



          Also, in order to count "Other" benefits, we need to ensure that the benefit IS NOT NULL and it is NOT IN ('SSS', 'Coco Life').



          In multi-table queries, it is advisable to use Aliasing for Code clarity (readability) and avoiding ambiguous behaviour.



          SELECT 
          c.Address,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Male' THEN c.ID END) AS male_cnt,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Female' THEN c.ID END) AS female_cnt,
          COUNT(DISTINCT c.ID) AS total_citizen_cnt,
          COUNT(CASE WHEN b.benefits = 'SSS' THEN 1 END) AS SSS_cnt,
          COUNT(CASE WHEN b.benefits = 'Coco Life' THEN 1 END) AS Coco_Life_cnt,
          COUNT(CASE WHEN b.benefits IS NOT NULL AND
          b.benefits NOT IN ('SSS', 'Coco Life') THEN 1 END) AS Others_cnt
          FROM citizen AS c
          LEFT JOIN pensioners AS p
          ON p.citizen_ID = c.ID
          LEFT JOIN benefits AS b
          ON b.ID = p.benefits_ID
          GROUP BY c.Address





          share|improve this answer
















          • 1




            Your too quick @Madhur. :D Let me try this piece. Thank you once again.
            – Kinanda Mata
            Nov 11 at 11:51










          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%2f53248405%2fi-want-to-display-a-summary-like-this-based-on-the-three-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








          up vote
          1
          down vote



          accepted










          You can do a Left Join from the Address table to the benefits table, via pensioners table, using the appropriate relationships. Left join will allows us to consider a Address even when there is no corresponding benefits entry for any of its citizens.



          In order to count total citizens, male count and female count, you now need to use COUNT(DISTINCT ID) after the join. As Joining may create duplicate rows, as a citizen may have more than one benefits.



          Also, in order to count "Other" benefits, we need to ensure that the benefit IS NOT NULL and it is NOT IN ('SSS', 'Coco Life').



          In multi-table queries, it is advisable to use Aliasing for Code clarity (readability) and avoiding ambiguous behaviour.



          SELECT 
          c.Address,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Male' THEN c.ID END) AS male_cnt,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Female' THEN c.ID END) AS female_cnt,
          COUNT(DISTINCT c.ID) AS total_citizen_cnt,
          COUNT(CASE WHEN b.benefits = 'SSS' THEN 1 END) AS SSS_cnt,
          COUNT(CASE WHEN b.benefits = 'Coco Life' THEN 1 END) AS Coco_Life_cnt,
          COUNT(CASE WHEN b.benefits IS NOT NULL AND
          b.benefits NOT IN ('SSS', 'Coco Life') THEN 1 END) AS Others_cnt
          FROM citizen AS c
          LEFT JOIN pensioners AS p
          ON p.citizen_ID = c.ID
          LEFT JOIN benefits AS b
          ON b.ID = p.benefits_ID
          GROUP BY c.Address





          share|improve this answer
















          • 1




            Your too quick @Madhur. :D Let me try this piece. Thank you once again.
            – Kinanda Mata
            Nov 11 at 11:51














          up vote
          1
          down vote



          accepted










          You can do a Left Join from the Address table to the benefits table, via pensioners table, using the appropriate relationships. Left join will allows us to consider a Address even when there is no corresponding benefits entry for any of its citizens.



          In order to count total citizens, male count and female count, you now need to use COUNT(DISTINCT ID) after the join. As Joining may create duplicate rows, as a citizen may have more than one benefits.



          Also, in order to count "Other" benefits, we need to ensure that the benefit IS NOT NULL and it is NOT IN ('SSS', 'Coco Life').



          In multi-table queries, it is advisable to use Aliasing for Code clarity (readability) and avoiding ambiguous behaviour.



          SELECT 
          c.Address,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Male' THEN c.ID END) AS male_cnt,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Female' THEN c.ID END) AS female_cnt,
          COUNT(DISTINCT c.ID) AS total_citizen_cnt,
          COUNT(CASE WHEN b.benefits = 'SSS' THEN 1 END) AS SSS_cnt,
          COUNT(CASE WHEN b.benefits = 'Coco Life' THEN 1 END) AS Coco_Life_cnt,
          COUNT(CASE WHEN b.benefits IS NOT NULL AND
          b.benefits NOT IN ('SSS', 'Coco Life') THEN 1 END) AS Others_cnt
          FROM citizen AS c
          LEFT JOIN pensioners AS p
          ON p.citizen_ID = c.ID
          LEFT JOIN benefits AS b
          ON b.ID = p.benefits_ID
          GROUP BY c.Address





          share|improve this answer
















          • 1




            Your too quick @Madhur. :D Let me try this piece. Thank you once again.
            – Kinanda Mata
            Nov 11 at 11:51












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can do a Left Join from the Address table to the benefits table, via pensioners table, using the appropriate relationships. Left join will allows us to consider a Address even when there is no corresponding benefits entry for any of its citizens.



          In order to count total citizens, male count and female count, you now need to use COUNT(DISTINCT ID) after the join. As Joining may create duplicate rows, as a citizen may have more than one benefits.



          Also, in order to count "Other" benefits, we need to ensure that the benefit IS NOT NULL and it is NOT IN ('SSS', 'Coco Life').



          In multi-table queries, it is advisable to use Aliasing for Code clarity (readability) and avoiding ambiguous behaviour.



          SELECT 
          c.Address,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Male' THEN c.ID END) AS male_cnt,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Female' THEN c.ID END) AS female_cnt,
          COUNT(DISTINCT c.ID) AS total_citizen_cnt,
          COUNT(CASE WHEN b.benefits = 'SSS' THEN 1 END) AS SSS_cnt,
          COUNT(CASE WHEN b.benefits = 'Coco Life' THEN 1 END) AS Coco_Life_cnt,
          COUNT(CASE WHEN b.benefits IS NOT NULL AND
          b.benefits NOT IN ('SSS', 'Coco Life') THEN 1 END) AS Others_cnt
          FROM citizen AS c
          LEFT JOIN pensioners AS p
          ON p.citizen_ID = c.ID
          LEFT JOIN benefits AS b
          ON b.ID = p.benefits_ID
          GROUP BY c.Address





          share|improve this answer












          You can do a Left Join from the Address table to the benefits table, via pensioners table, using the appropriate relationships. Left join will allows us to consider a Address even when there is no corresponding benefits entry for any of its citizens.



          In order to count total citizens, male count and female count, you now need to use COUNT(DISTINCT ID) after the join. As Joining may create duplicate rows, as a citizen may have more than one benefits.



          Also, in order to count "Other" benefits, we need to ensure that the benefit IS NOT NULL and it is NOT IN ('SSS', 'Coco Life').



          In multi-table queries, it is advisable to use Aliasing for Code clarity (readability) and avoiding ambiguous behaviour.



          SELECT 
          c.Address,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Male' THEN c.ID END) AS male_cnt,
          COUNT(DISTINCT CASE WHEN c.Sex = 'Female' THEN c.ID END) AS female_cnt,
          COUNT(DISTINCT c.ID) AS total_citizen_cnt,
          COUNT(CASE WHEN b.benefits = 'SSS' THEN 1 END) AS SSS_cnt,
          COUNT(CASE WHEN b.benefits = 'Coco Life' THEN 1 END) AS Coco_Life_cnt,
          COUNT(CASE WHEN b.benefits IS NOT NULL AND
          b.benefits NOT IN ('SSS', 'Coco Life') THEN 1 END) AS Others_cnt
          FROM citizen AS c
          LEFT JOIN pensioners AS p
          ON p.citizen_ID = c.ID
          LEFT JOIN benefits AS b
          ON b.ID = p.benefits_ID
          GROUP BY c.Address






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 11:46









          Madhur Bhaiya

          18.8k62236




          18.8k62236







          • 1




            Your too quick @Madhur. :D Let me try this piece. Thank you once again.
            – Kinanda Mata
            Nov 11 at 11:51












          • 1




            Your too quick @Madhur. :D Let me try this piece. Thank you once again.
            – Kinanda Mata
            Nov 11 at 11:51







          1




          1




          Your too quick @Madhur. :D Let me try this piece. Thank you once again.
          – Kinanda Mata
          Nov 11 at 11:51




          Your too quick @Madhur. :D Let me try this piece. Thank you once again.
          – Kinanda Mata
          Nov 11 at 11:51

















          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%2f53248405%2fi-want-to-display-a-summary-like-this-based-on-the-three-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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto