mysql join with max value inside group by

Multi tool use
Multi tool use











up vote
1
down vote

favorite












I am attempting to write a query.



My table is:



+----+---------+------------+
| id | user_id | date |
+----+---------+------------+
| 1 | 1 | 2013-04-01 |
| 2 | 1 | 2017-01-01 |
| 3 | 1 | 2018-07-01 |
| 4 | 2 | 2018-09-01 |
| 5 | 2 | 2018-05-01 |
| 6 | 3 | 2018-10-01 |
| 7 | 1 | 2012-01-01 |
| 8 | 3 | 2016-06-01 |
| 9 | 3 | 2011-01-01 |
| 10 | 1 | 2000-01-01 |
+----+---------+------------+


Expected Result:



+----+---------+------------+-------------+
| id | user_id | date | first_login |
+----+---------+------------+-------------+
| 1 | 1 | 2013-04-01 | 2000-01-01 |
| 2 | 1 | 2017-01-01 | 2000-01-01 |
| 3 | 1 | 2018-07-01 | 2000-01-01 |
| 4 | 2 | 2018-09-01 | 2018-05-01 |
| 5 | 2 | 2018-05-01 | 2018-05-01 |
| 6 | 3 | 2018-10-01 | 2011-01-01 |
| 7 | 1 | 2012-01-01 | 2000-01-01 |
| 8 | 3 | 2016-06-01 | 2011-01-01 |
| 9 | 3 | 2011-01-01 | 2011-01-01 |
| 10 | 1 | 2000-01-01 | 2000-01-01 |
+----+---------+------------+-------------+


Is it possible ?
it seems to me that it's a combination of joins !



I tried that, but it does not work.



CREATE TABLE CONNEXIONS
(`id` int, `user_id` int, `date` date)
;

INSERT INTO CONNEXIONS
(`id`, `user_id`, `date`)
VALUES
(1, 1, '2013-04-01'),
(2, 1, '2017-01-01'),
(3, 1, '2018-07-01'),
(4, 2, '2018-09-01'),
(5, 2, '2018-05-01'),
(6, 3, '2018-10-01'),
(7, 1, '2012-01-01'),
(8, 3, '2016-06-01'),
(9, 3, '2011-01-01'),
(0, 1, '2000-01-01')
;


SELECT conn.*, c.first_login
FROM CONNEXIONS

INNER JOIN (
SELECT MAX(conn.date) AS first_login, user_id
FROM CONNEXIONS AS conn
GROUP BY conn.client_id
) c ON (conn.user_id = c.client_id)








share
























  • "but it does not work" - what does that mean? Do you get any error message? And what is 'client_id'?
    – Paul Spiegel
    1 min ago















up vote
1
down vote

favorite












I am attempting to write a query.



My table is:



+----+---------+------------+
| id | user_id | date |
+----+---------+------------+
| 1 | 1 | 2013-04-01 |
| 2 | 1 | 2017-01-01 |
| 3 | 1 | 2018-07-01 |
| 4 | 2 | 2018-09-01 |
| 5 | 2 | 2018-05-01 |
| 6 | 3 | 2018-10-01 |
| 7 | 1 | 2012-01-01 |
| 8 | 3 | 2016-06-01 |
| 9 | 3 | 2011-01-01 |
| 10 | 1 | 2000-01-01 |
+----+---------+------------+


Expected Result:



+----+---------+------------+-------------+
| id | user_id | date | first_login |
+----+---------+------------+-------------+
| 1 | 1 | 2013-04-01 | 2000-01-01 |
| 2 | 1 | 2017-01-01 | 2000-01-01 |
| 3 | 1 | 2018-07-01 | 2000-01-01 |
| 4 | 2 | 2018-09-01 | 2018-05-01 |
| 5 | 2 | 2018-05-01 | 2018-05-01 |
| 6 | 3 | 2018-10-01 | 2011-01-01 |
| 7 | 1 | 2012-01-01 | 2000-01-01 |
| 8 | 3 | 2016-06-01 | 2011-01-01 |
| 9 | 3 | 2011-01-01 | 2011-01-01 |
| 10 | 1 | 2000-01-01 | 2000-01-01 |
+----+---------+------------+-------------+


Is it possible ?
it seems to me that it's a combination of joins !



I tried that, but it does not work.



CREATE TABLE CONNEXIONS
(`id` int, `user_id` int, `date` date)
;

INSERT INTO CONNEXIONS
(`id`, `user_id`, `date`)
VALUES
(1, 1, '2013-04-01'),
(2, 1, '2017-01-01'),
(3, 1, '2018-07-01'),
(4, 2, '2018-09-01'),
(5, 2, '2018-05-01'),
(6, 3, '2018-10-01'),
(7, 1, '2012-01-01'),
(8, 3, '2016-06-01'),
(9, 3, '2011-01-01'),
(0, 1, '2000-01-01')
;


SELECT conn.*, c.first_login
FROM CONNEXIONS

INNER JOIN (
SELECT MAX(conn.date) AS first_login, user_id
FROM CONNEXIONS AS conn
GROUP BY conn.client_id
) c ON (conn.user_id = c.client_id)








share
























  • "but it does not work" - what does that mean? Do you get any error message? And what is 'client_id'?
    – Paul Spiegel
    1 min ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am attempting to write a query.



My table is:



+----+---------+------------+
| id | user_id | date |
+----+---------+------------+
| 1 | 1 | 2013-04-01 |
| 2 | 1 | 2017-01-01 |
| 3 | 1 | 2018-07-01 |
| 4 | 2 | 2018-09-01 |
| 5 | 2 | 2018-05-01 |
| 6 | 3 | 2018-10-01 |
| 7 | 1 | 2012-01-01 |
| 8 | 3 | 2016-06-01 |
| 9 | 3 | 2011-01-01 |
| 10 | 1 | 2000-01-01 |
+----+---------+------------+


Expected Result:



+----+---------+------------+-------------+
| id | user_id | date | first_login |
+----+---------+------------+-------------+
| 1 | 1 | 2013-04-01 | 2000-01-01 |
| 2 | 1 | 2017-01-01 | 2000-01-01 |
| 3 | 1 | 2018-07-01 | 2000-01-01 |
| 4 | 2 | 2018-09-01 | 2018-05-01 |
| 5 | 2 | 2018-05-01 | 2018-05-01 |
| 6 | 3 | 2018-10-01 | 2011-01-01 |
| 7 | 1 | 2012-01-01 | 2000-01-01 |
| 8 | 3 | 2016-06-01 | 2011-01-01 |
| 9 | 3 | 2011-01-01 | 2011-01-01 |
| 10 | 1 | 2000-01-01 | 2000-01-01 |
+----+---------+------------+-------------+


Is it possible ?
it seems to me that it's a combination of joins !



I tried that, but it does not work.



CREATE TABLE CONNEXIONS
(`id` int, `user_id` int, `date` date)
;

INSERT INTO CONNEXIONS
(`id`, `user_id`, `date`)
VALUES
(1, 1, '2013-04-01'),
(2, 1, '2017-01-01'),
(3, 1, '2018-07-01'),
(4, 2, '2018-09-01'),
(5, 2, '2018-05-01'),
(6, 3, '2018-10-01'),
(7, 1, '2012-01-01'),
(8, 3, '2016-06-01'),
(9, 3, '2011-01-01'),
(0, 1, '2000-01-01')
;


SELECT conn.*, c.first_login
FROM CONNEXIONS

INNER JOIN (
SELECT MAX(conn.date) AS first_login, user_id
FROM CONNEXIONS AS conn
GROUP BY conn.client_id
) c ON (conn.user_id = c.client_id)








share















I am attempting to write a query.



My table is:



+----+---------+------------+
| id | user_id | date |
+----+---------+------------+
| 1 | 1 | 2013-04-01 |
| 2 | 1 | 2017-01-01 |
| 3 | 1 | 2018-07-01 |
| 4 | 2 | 2018-09-01 |
| 5 | 2 | 2018-05-01 |
| 6 | 3 | 2018-10-01 |
| 7 | 1 | 2012-01-01 |
| 8 | 3 | 2016-06-01 |
| 9 | 3 | 2011-01-01 |
| 10 | 1 | 2000-01-01 |
+----+---------+------------+


Expected Result:



+----+---------+------------+-------------+
| id | user_id | date | first_login |
+----+---------+------------+-------------+
| 1 | 1 | 2013-04-01 | 2000-01-01 |
| 2 | 1 | 2017-01-01 | 2000-01-01 |
| 3 | 1 | 2018-07-01 | 2000-01-01 |
| 4 | 2 | 2018-09-01 | 2018-05-01 |
| 5 | 2 | 2018-05-01 | 2018-05-01 |
| 6 | 3 | 2018-10-01 | 2011-01-01 |
| 7 | 1 | 2012-01-01 | 2000-01-01 |
| 8 | 3 | 2016-06-01 | 2011-01-01 |
| 9 | 3 | 2011-01-01 | 2011-01-01 |
| 10 | 1 | 2000-01-01 | 2000-01-01 |
+----+---------+------------+-------------+


Is it possible ?
it seems to me that it's a combination of joins !



I tried that, but it does not work.



CREATE TABLE CONNEXIONS
(`id` int, `user_id` int, `date` date)
;

INSERT INTO CONNEXIONS
(`id`, `user_id`, `date`)
VALUES
(1, 1, '2013-04-01'),
(2, 1, '2017-01-01'),
(3, 1, '2018-07-01'),
(4, 2, '2018-09-01'),
(5, 2, '2018-05-01'),
(6, 3, '2018-10-01'),
(7, 1, '2012-01-01'),
(8, 3, '2016-06-01'),
(9, 3, '2011-01-01'),
(0, 1, '2000-01-01')
;


SELECT conn.*, c.first_login
FROM CONNEXIONS

INNER JOIN (
SELECT MAX(conn.date) AS first_login, user_id
FROM CONNEXIONS AS conn
GROUP BY conn.client_id
) c ON (conn.user_id = c.client_id)






mysql sql left-join





share














share












share



share








edited 8 mins ago

























asked 9 mins ago









oussaka

57359




57359












  • "but it does not work" - what does that mean? Do you get any error message? And what is 'client_id'?
    – Paul Spiegel
    1 min ago


















  • "but it does not work" - what does that mean? Do you get any error message? And what is 'client_id'?
    – Paul Spiegel
    1 min ago
















"but it does not work" - what does that mean? Do you get any error message? And what is 'client_id'?
– Paul Spiegel
1 min ago




"but it does not work" - what does that mean? Do you get any error message? And what is 'client_id'?
– Paul Spiegel
1 min ago












3 Answers
3






active

oldest

votes

















up vote
1
down vote














  • I believe that the first_login will be the minimum connection date, not the maximum.

  • You aliasing was misplaced. I have fixed it.


Try:



SELECT  conn1.*, c.first_login
FROM CONNEXIONS AS conn1
INNER JOIN (
SELECT MIN(conn2.date) AS first_login, conn2.user_id
FROM CONNEXIONS AS conn2
GROUP BY conn2.user_id
) c ON (conn1.user_id = c.user_id)




Result



| id  | user_id | date       | first_login |
| --- | ------- | ---------- | ----------- |
| 1 | 1 | 2013-04-01 | 2000-01-01 |
| 2 | 1 | 2017-01-01 | 2000-01-01 |
| 3 | 1 | 2018-07-01 | 2000-01-01 |
| 4 | 2 | 2018-09-01 | 2018-05-01 |
| 5 | 2 | 2018-05-01 | 2018-05-01 |
| 6 | 3 | 2018-10-01 | 2011-01-01 |
| 7 | 1 | 2012-01-01 | 2000-01-01 |
| 8 | 3 | 2016-06-01 | 2011-01-01 |
| 9 | 3 | 2011-01-01 | 2011-01-01 |
| 0 | 1 | 2000-01-01 | 2000-01-01 |


View on DB Fiddle





share




























    up vote
    1
    down vote













    You can use correlated subquery & you need MIN() instead of MAX() :



    SELECT CONN.*,
    (SELECT MIN(conn1.date)
    FROM CONNEXIONS conn1
    WHERE conn1.user_id = conn.user_id
    ) AS first_login
    FROM CONNEXIONS conn;




    share




























      up vote
      1
      down vote













      just use min() instead max()



      SELECT  conn.*, c.first_login
      FROM CONNEXIONS

      INNER JOIN (
      SELECT min(conn.date) AS first_login, user_id
      FROM CONNEXIONS AS conn
      GROUP BY conn.user_id
      ) c ON (conn.user_id = c.client_id)




      share























      • thank you but the question was rather about the join, not about MIN / MAX
        – oussaka
        1 min ago











      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%2f53204471%2fmysql-join-with-max-value-inside-group-by%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote














      • I believe that the first_login will be the minimum connection date, not the maximum.

      • You aliasing was misplaced. I have fixed it.


      Try:



      SELECT  conn1.*, c.first_login
      FROM CONNEXIONS AS conn1
      INNER JOIN (
      SELECT MIN(conn2.date) AS first_login, conn2.user_id
      FROM CONNEXIONS AS conn2
      GROUP BY conn2.user_id
      ) c ON (conn1.user_id = c.user_id)




      Result



      | id  | user_id | date       | first_login |
      | --- | ------- | ---------- | ----------- |
      | 1 | 1 | 2013-04-01 | 2000-01-01 |
      | 2 | 1 | 2017-01-01 | 2000-01-01 |
      | 3 | 1 | 2018-07-01 | 2000-01-01 |
      | 4 | 2 | 2018-09-01 | 2018-05-01 |
      | 5 | 2 | 2018-05-01 | 2018-05-01 |
      | 6 | 3 | 2018-10-01 | 2011-01-01 |
      | 7 | 1 | 2012-01-01 | 2000-01-01 |
      | 8 | 3 | 2016-06-01 | 2011-01-01 |
      | 9 | 3 | 2011-01-01 | 2011-01-01 |
      | 0 | 1 | 2000-01-01 | 2000-01-01 |


      View on DB Fiddle





      share

























        up vote
        1
        down vote














        • I believe that the first_login will be the minimum connection date, not the maximum.

        • You aliasing was misplaced. I have fixed it.


        Try:



        SELECT  conn1.*, c.first_login
        FROM CONNEXIONS AS conn1
        INNER JOIN (
        SELECT MIN(conn2.date) AS first_login, conn2.user_id
        FROM CONNEXIONS AS conn2
        GROUP BY conn2.user_id
        ) c ON (conn1.user_id = c.user_id)




        Result



        | id  | user_id | date       | first_login |
        | --- | ------- | ---------- | ----------- |
        | 1 | 1 | 2013-04-01 | 2000-01-01 |
        | 2 | 1 | 2017-01-01 | 2000-01-01 |
        | 3 | 1 | 2018-07-01 | 2000-01-01 |
        | 4 | 2 | 2018-09-01 | 2018-05-01 |
        | 5 | 2 | 2018-05-01 | 2018-05-01 |
        | 6 | 3 | 2018-10-01 | 2011-01-01 |
        | 7 | 1 | 2012-01-01 | 2000-01-01 |
        | 8 | 3 | 2016-06-01 | 2011-01-01 |
        | 9 | 3 | 2011-01-01 | 2011-01-01 |
        | 0 | 1 | 2000-01-01 | 2000-01-01 |


        View on DB Fiddle





        share























          up vote
          1
          down vote










          up vote
          1
          down vote










          • I believe that the first_login will be the minimum connection date, not the maximum.

          • You aliasing was misplaced. I have fixed it.


          Try:



          SELECT  conn1.*, c.first_login
          FROM CONNEXIONS AS conn1
          INNER JOIN (
          SELECT MIN(conn2.date) AS first_login, conn2.user_id
          FROM CONNEXIONS AS conn2
          GROUP BY conn2.user_id
          ) c ON (conn1.user_id = c.user_id)




          Result



          | id  | user_id | date       | first_login |
          | --- | ------- | ---------- | ----------- |
          | 1 | 1 | 2013-04-01 | 2000-01-01 |
          | 2 | 1 | 2017-01-01 | 2000-01-01 |
          | 3 | 1 | 2018-07-01 | 2000-01-01 |
          | 4 | 2 | 2018-09-01 | 2018-05-01 |
          | 5 | 2 | 2018-05-01 | 2018-05-01 |
          | 6 | 3 | 2018-10-01 | 2011-01-01 |
          | 7 | 1 | 2012-01-01 | 2000-01-01 |
          | 8 | 3 | 2016-06-01 | 2011-01-01 |
          | 9 | 3 | 2011-01-01 | 2011-01-01 |
          | 0 | 1 | 2000-01-01 | 2000-01-01 |


          View on DB Fiddle





          share













          • I believe that the first_login will be the minimum connection date, not the maximum.

          • You aliasing was misplaced. I have fixed it.


          Try:



          SELECT  conn1.*, c.first_login
          FROM CONNEXIONS AS conn1
          INNER JOIN (
          SELECT MIN(conn2.date) AS first_login, conn2.user_id
          FROM CONNEXIONS AS conn2
          GROUP BY conn2.user_id
          ) c ON (conn1.user_id = c.user_id)




          Result



          | id  | user_id | date       | first_login |
          | --- | ------- | ---------- | ----------- |
          | 1 | 1 | 2013-04-01 | 2000-01-01 |
          | 2 | 1 | 2017-01-01 | 2000-01-01 |
          | 3 | 1 | 2018-07-01 | 2000-01-01 |
          | 4 | 2 | 2018-09-01 | 2018-05-01 |
          | 5 | 2 | 2018-05-01 | 2018-05-01 |
          | 6 | 3 | 2018-10-01 | 2011-01-01 |
          | 7 | 1 | 2012-01-01 | 2000-01-01 |
          | 8 | 3 | 2016-06-01 | 2011-01-01 |
          | 9 | 3 | 2011-01-01 | 2011-01-01 |
          | 0 | 1 | 2000-01-01 | 2000-01-01 |


          View on DB Fiddle






          share











          share


          share










          answered 8 mins ago









          Madhur Bhaiya

          13.2k42035




          13.2k42035
























              up vote
              1
              down vote













              You can use correlated subquery & you need MIN() instead of MAX() :



              SELECT CONN.*,
              (SELECT MIN(conn1.date)
              FROM CONNEXIONS conn1
              WHERE conn1.user_id = conn.user_id
              ) AS first_login
              FROM CONNEXIONS conn;




              share

























                up vote
                1
                down vote













                You can use correlated subquery & you need MIN() instead of MAX() :



                SELECT CONN.*,
                (SELECT MIN(conn1.date)
                FROM CONNEXIONS conn1
                WHERE conn1.user_id = conn.user_id
                ) AS first_login
                FROM CONNEXIONS conn;




                share























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can use correlated subquery & you need MIN() instead of MAX() :



                  SELECT CONN.*,
                  (SELECT MIN(conn1.date)
                  FROM CONNEXIONS conn1
                  WHERE conn1.user_id = conn.user_id
                  ) AS first_login
                  FROM CONNEXIONS conn;




                  share












                  You can use correlated subquery & you need MIN() instead of MAX() :



                  SELECT CONN.*,
                  (SELECT MIN(conn1.date)
                  FROM CONNEXIONS conn1
                  WHERE conn1.user_id = conn.user_id
                  ) AS first_login
                  FROM CONNEXIONS conn;





                  share











                  share


                  share










                  answered 6 mins ago









                  Yogesh Sharma

                  25.6k51234




                  25.6k51234






















                      up vote
                      1
                      down vote













                      just use min() instead max()



                      SELECT  conn.*, c.first_login
                      FROM CONNEXIONS

                      INNER JOIN (
                      SELECT min(conn.date) AS first_login, user_id
                      FROM CONNEXIONS AS conn
                      GROUP BY conn.user_id
                      ) c ON (conn.user_id = c.client_id)




                      share























                      • thank you but the question was rather about the join, not about MIN / MAX
                        – oussaka
                        1 min ago















                      up vote
                      1
                      down vote













                      just use min() instead max()



                      SELECT  conn.*, c.first_login
                      FROM CONNEXIONS

                      INNER JOIN (
                      SELECT min(conn.date) AS first_login, user_id
                      FROM CONNEXIONS AS conn
                      GROUP BY conn.user_id
                      ) c ON (conn.user_id = c.client_id)




                      share























                      • thank you but the question was rather about the join, not about MIN / MAX
                        – oussaka
                        1 min ago













                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      just use min() instead max()



                      SELECT  conn.*, c.first_login
                      FROM CONNEXIONS

                      INNER JOIN (
                      SELECT min(conn.date) AS first_login, user_id
                      FROM CONNEXIONS AS conn
                      GROUP BY conn.user_id
                      ) c ON (conn.user_id = c.client_id)




                      share














                      just use min() instead max()



                      SELECT  conn.*, c.first_login
                      FROM CONNEXIONS

                      INNER JOIN (
                      SELECT min(conn.date) AS first_login, user_id
                      FROM CONNEXIONS AS conn
                      GROUP BY conn.user_id
                      ) c ON (conn.user_id = c.client_id)





                      share













                      share


                      share








                      edited 1 min ago

























                      answered 6 mins ago









                      Zaynul Abadin Tuhin

                      9,7922730




                      9,7922730












                      • thank you but the question was rather about the join, not about MIN / MAX
                        – oussaka
                        1 min ago


















                      • thank you but the question was rather about the join, not about MIN / MAX
                        – oussaka
                        1 min ago
















                      thank you but the question was rather about the join, not about MIN / MAX
                      – oussaka
                      1 min ago




                      thank you but the question was rather about the join, not about MIN / MAX
                      – oussaka
                      1 min ago


















                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204471%2fmysql-join-with-max-value-inside-group-by%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      D,0tqcuAKVkU64XLShN,v 9dLEx5Dvuj rbtLu5MOG8nvP6g1DHFuzi4WOm
                      EiECeu iCTGg4eJ1E,Rs kbRYphcxRb1YYmhNh,gbbQ5Z77Pq3NC,fC0oEogH

                      Popular posts from this blog

                      How to pass form data using jquery Ajax to insert data in database?

                      Guess what letter conforming each word

                      Run scheduled task as local user group (not BUILTIN)