mysql join with max value inside group by
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)
mysql sql left-join
add a comment |
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)
mysql sql left-join
"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
add a comment |
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)
mysql sql left-join
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
mysql sql left-join
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
add a comment |
"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
add a comment |
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
add a comment |
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;
add a comment |
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)
thank you but the question was rather about the join, not about MIN / MAX
– oussaka
1 min ago
add a comment |
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
add a comment |
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
add a comment |
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
- 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
answered 8 mins ago
Madhur Bhaiya
13.2k42035
13.2k42035
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered 6 mins ago
Yogesh Sharma
25.6k51234
25.6k51234
add a comment |
add a comment |
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)
thank you but the question was rather about the join, not about MIN / MAX
– oussaka
1 min ago
add a comment |
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)
thank you but the question was rather about the join, not about MIN / MAX
– oussaka
1 min ago
add a comment |
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)
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)
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
"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