MySQL Display All Date between range altough is no record












1















I Have Table xDateList Contain:



+---------+
xDateList
+---------+
2018-11-01
2018-11-02
2018-11-03
2018-11-04
2018-11-05


And Also Table ScanLog



--------------------------------------
ID Name ScanDate Code
--------------------------------------
1 John 2018-11-02 07:00:00 IN
1 John 2018-11-02 10:00:00 OUT
1 John 2018-11-04 08:00:00 IN
1 John 2018-11-04 12:00:00 OUT


I have tried this but it cannot display all record on xDateList, it only show record on table ScanLog



select xDateList.date, 
scanlog.name,
MIN(scanlog.scandate) AS `IN`,
MAX(scanlog.scandate) AS `OUT`
from scanlog
left JOIN xDateList ON xDateList.date = date(scanlog.scandate)
where scanlog.id='1'
GROUP BY DATE(scanlog.scandate)


I want result like this



--------------------------------------------
Date ID Name In Out
--------------------------------------------
2018-11-01 1 John
2018-11-02 1 John 07:00:00 10:00:00
2018-11-03 1 John
2018-11-04 1 John 08:00:00 12:00:00
2018-11-05 1 John


Thankyou for helping me










share|improve this question





























    1















    I Have Table xDateList Contain:



    +---------+
    xDateList
    +---------+
    2018-11-01
    2018-11-02
    2018-11-03
    2018-11-04
    2018-11-05


    And Also Table ScanLog



    --------------------------------------
    ID Name ScanDate Code
    --------------------------------------
    1 John 2018-11-02 07:00:00 IN
    1 John 2018-11-02 10:00:00 OUT
    1 John 2018-11-04 08:00:00 IN
    1 John 2018-11-04 12:00:00 OUT


    I have tried this but it cannot display all record on xDateList, it only show record on table ScanLog



    select xDateList.date, 
    scanlog.name,
    MIN(scanlog.scandate) AS `IN`,
    MAX(scanlog.scandate) AS `OUT`
    from scanlog
    left JOIN xDateList ON xDateList.date = date(scanlog.scandate)
    where scanlog.id='1'
    GROUP BY DATE(scanlog.scandate)


    I want result like this



    --------------------------------------------
    Date ID Name In Out
    --------------------------------------------
    2018-11-01 1 John
    2018-11-02 1 John 07:00:00 10:00:00
    2018-11-03 1 John
    2018-11-04 1 John 08:00:00 12:00:00
    2018-11-05 1 John


    Thankyou for helping me










    share|improve this question



























      1












      1








      1








      I Have Table xDateList Contain:



      +---------+
      xDateList
      +---------+
      2018-11-01
      2018-11-02
      2018-11-03
      2018-11-04
      2018-11-05


      And Also Table ScanLog



      --------------------------------------
      ID Name ScanDate Code
      --------------------------------------
      1 John 2018-11-02 07:00:00 IN
      1 John 2018-11-02 10:00:00 OUT
      1 John 2018-11-04 08:00:00 IN
      1 John 2018-11-04 12:00:00 OUT


      I have tried this but it cannot display all record on xDateList, it only show record on table ScanLog



      select xDateList.date, 
      scanlog.name,
      MIN(scanlog.scandate) AS `IN`,
      MAX(scanlog.scandate) AS `OUT`
      from scanlog
      left JOIN xDateList ON xDateList.date = date(scanlog.scandate)
      where scanlog.id='1'
      GROUP BY DATE(scanlog.scandate)


      I want result like this



      --------------------------------------------
      Date ID Name In Out
      --------------------------------------------
      2018-11-01 1 John
      2018-11-02 1 John 07:00:00 10:00:00
      2018-11-03 1 John
      2018-11-04 1 John 08:00:00 12:00:00
      2018-11-05 1 John


      Thankyou for helping me










      share|improve this question
















      I Have Table xDateList Contain:



      +---------+
      xDateList
      +---------+
      2018-11-01
      2018-11-02
      2018-11-03
      2018-11-04
      2018-11-05


      And Also Table ScanLog



      --------------------------------------
      ID Name ScanDate Code
      --------------------------------------
      1 John 2018-11-02 07:00:00 IN
      1 John 2018-11-02 10:00:00 OUT
      1 John 2018-11-04 08:00:00 IN
      1 John 2018-11-04 12:00:00 OUT


      I have tried this but it cannot display all record on xDateList, it only show record on table ScanLog



      select xDateList.date, 
      scanlog.name,
      MIN(scanlog.scandate) AS `IN`,
      MAX(scanlog.scandate) AS `OUT`
      from scanlog
      left JOIN xDateList ON xDateList.date = date(scanlog.scandate)
      where scanlog.id='1'
      GROUP BY DATE(scanlog.scandate)


      I want result like this



      --------------------------------------------
      Date ID Name In Out
      --------------------------------------------
      2018-11-01 1 John
      2018-11-02 1 John 07:00:00 10:00:00
      2018-11-03 1 John
      2018-11-04 1 John 08:00:00 12:00:00
      2018-11-05 1 John


      Thankyou for helping me







      mysql date






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 8:54









      Madhur Bhaiya

      19.6k62236




      19.6k62236










      asked Nov 28 '18 at 2:56









      FranzFranz

      82




      82
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You need to change the order of tables in the LEFT JOIN. Always remember that in order to consider all the rows from a particular table; that particular table should be the left-most table in the Join.



          Also, whenever doing a LEFT JOIN, conditions on the right side table should be specified in the ON clause; otherwise the conditions in WHERE clause can effectively turn it to INNER JOIN instead.



          Also, in this case, the GROUP BY should be on the xDateList.date to show all the rows corresponding to xDateList.date values. And, we need to ensure that all non-aggregated columns in the SELECT list are specified in the GROUP BY clause as well. Do check: Error related to only_full_group_by when executing a query in MySql



          SELECT xDateList.date, 
          scanlog.name,
          MIN(scanlog.scandate) AS `IN`,
          MAX(scanlog.scandate) AS `OUT`
          FROM xDateList
          LEFT JOIN scanlog
          ON xDateList.date = date(scanlog.scandate) AND
          scanlog.id='1'
          GROUP BY xDateList.date, scanlog.name


          Result



          | date       | name | IN                  | OUT                 |
          | ---------- | ---- | ------------------- | ------------------- |
          | 2018-11-01 | | | |
          | 2018-11-02 | John | 2018-11-02 07:00:00 | 2018-11-02 10:00:00 |
          | 2018-11-03 | | | |
          | 2018-11-04 | John | 2018-11-04 08:00:00 | 2018-11-04 12:00:00 |
          | 2018-11-05 | | | |


          View on DB Fiddle






          share|improve this answer

























            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%2f53511424%2fmysql-display-all-date-between-range-altough-is-no-record%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














            You need to change the order of tables in the LEFT JOIN. Always remember that in order to consider all the rows from a particular table; that particular table should be the left-most table in the Join.



            Also, whenever doing a LEFT JOIN, conditions on the right side table should be specified in the ON clause; otherwise the conditions in WHERE clause can effectively turn it to INNER JOIN instead.



            Also, in this case, the GROUP BY should be on the xDateList.date to show all the rows corresponding to xDateList.date values. And, we need to ensure that all non-aggregated columns in the SELECT list are specified in the GROUP BY clause as well. Do check: Error related to only_full_group_by when executing a query in MySql



            SELECT xDateList.date, 
            scanlog.name,
            MIN(scanlog.scandate) AS `IN`,
            MAX(scanlog.scandate) AS `OUT`
            FROM xDateList
            LEFT JOIN scanlog
            ON xDateList.date = date(scanlog.scandate) AND
            scanlog.id='1'
            GROUP BY xDateList.date, scanlog.name


            Result



            | date       | name | IN                  | OUT                 |
            | ---------- | ---- | ------------------- | ------------------- |
            | 2018-11-01 | | | |
            | 2018-11-02 | John | 2018-11-02 07:00:00 | 2018-11-02 10:00:00 |
            | 2018-11-03 | | | |
            | 2018-11-04 | John | 2018-11-04 08:00:00 | 2018-11-04 12:00:00 |
            | 2018-11-05 | | | |


            View on DB Fiddle






            share|improve this answer






























              0














              You need to change the order of tables in the LEFT JOIN. Always remember that in order to consider all the rows from a particular table; that particular table should be the left-most table in the Join.



              Also, whenever doing a LEFT JOIN, conditions on the right side table should be specified in the ON clause; otherwise the conditions in WHERE clause can effectively turn it to INNER JOIN instead.



              Also, in this case, the GROUP BY should be on the xDateList.date to show all the rows corresponding to xDateList.date values. And, we need to ensure that all non-aggregated columns in the SELECT list are specified in the GROUP BY clause as well. Do check: Error related to only_full_group_by when executing a query in MySql



              SELECT xDateList.date, 
              scanlog.name,
              MIN(scanlog.scandate) AS `IN`,
              MAX(scanlog.scandate) AS `OUT`
              FROM xDateList
              LEFT JOIN scanlog
              ON xDateList.date = date(scanlog.scandate) AND
              scanlog.id='1'
              GROUP BY xDateList.date, scanlog.name


              Result



              | date       | name | IN                  | OUT                 |
              | ---------- | ---- | ------------------- | ------------------- |
              | 2018-11-01 | | | |
              | 2018-11-02 | John | 2018-11-02 07:00:00 | 2018-11-02 10:00:00 |
              | 2018-11-03 | | | |
              | 2018-11-04 | John | 2018-11-04 08:00:00 | 2018-11-04 12:00:00 |
              | 2018-11-05 | | | |


              View on DB Fiddle






              share|improve this answer




























                0












                0








                0







                You need to change the order of tables in the LEFT JOIN. Always remember that in order to consider all the rows from a particular table; that particular table should be the left-most table in the Join.



                Also, whenever doing a LEFT JOIN, conditions on the right side table should be specified in the ON clause; otherwise the conditions in WHERE clause can effectively turn it to INNER JOIN instead.



                Also, in this case, the GROUP BY should be on the xDateList.date to show all the rows corresponding to xDateList.date values. And, we need to ensure that all non-aggregated columns in the SELECT list are specified in the GROUP BY clause as well. Do check: Error related to only_full_group_by when executing a query in MySql



                SELECT xDateList.date, 
                scanlog.name,
                MIN(scanlog.scandate) AS `IN`,
                MAX(scanlog.scandate) AS `OUT`
                FROM xDateList
                LEFT JOIN scanlog
                ON xDateList.date = date(scanlog.scandate) AND
                scanlog.id='1'
                GROUP BY xDateList.date, scanlog.name


                Result



                | date       | name | IN                  | OUT                 |
                | ---------- | ---- | ------------------- | ------------------- |
                | 2018-11-01 | | | |
                | 2018-11-02 | John | 2018-11-02 07:00:00 | 2018-11-02 10:00:00 |
                | 2018-11-03 | | | |
                | 2018-11-04 | John | 2018-11-04 08:00:00 | 2018-11-04 12:00:00 |
                | 2018-11-05 | | | |


                View on DB Fiddle






                share|improve this answer















                You need to change the order of tables in the LEFT JOIN. Always remember that in order to consider all the rows from a particular table; that particular table should be the left-most table in the Join.



                Also, whenever doing a LEFT JOIN, conditions on the right side table should be specified in the ON clause; otherwise the conditions in WHERE clause can effectively turn it to INNER JOIN instead.



                Also, in this case, the GROUP BY should be on the xDateList.date to show all the rows corresponding to xDateList.date values. And, we need to ensure that all non-aggregated columns in the SELECT list are specified in the GROUP BY clause as well. Do check: Error related to only_full_group_by when executing a query in MySql



                SELECT xDateList.date, 
                scanlog.name,
                MIN(scanlog.scandate) AS `IN`,
                MAX(scanlog.scandate) AS `OUT`
                FROM xDateList
                LEFT JOIN scanlog
                ON xDateList.date = date(scanlog.scandate) AND
                scanlog.id='1'
                GROUP BY xDateList.date, scanlog.name


                Result



                | date       | name | IN                  | OUT                 |
                | ---------- | ---- | ------------------- | ------------------- |
                | 2018-11-01 | | | |
                | 2018-11-02 | John | 2018-11-02 07:00:00 | 2018-11-02 10:00:00 |
                | 2018-11-03 | | | |
                | 2018-11-04 | John | 2018-11-04 08:00:00 | 2018-11-04 12:00:00 |
                | 2018-11-05 | | | |


                View on DB Fiddle







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 28 '18 at 9:07

























                answered Nov 28 '18 at 3:04









                Madhur BhaiyaMadhur Bhaiya

                19.6k62236




                19.6k62236
































                    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%2f53511424%2fmysql-display-all-date-between-range-altough-is-no-record%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

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks