admin管理员组文章数量:1279055
I am trying to get the oldest date while using a GROUP BY
.
Using MySQL 5.7, I have a table called submissions. I can write a query the returns data like this:
SELECT `email_id`,
`message_id`,
`submit_dt`
FROM `submissions`
WHERE `email_id` = '10';
The above query returns 3 records that look like this:
| email_id | message_id | submit_dt |
-----------------------------------------------
| 10 | 1234 | 2025-01-23 13:30:01 |
-----------------------------------------------
| 10 | 1234 | 2025-01-23 14:00:01 |
-----------------------------------------------
| 10 | 2345 | 2025-02-08 13:00:02 |
-----------------------------------------------
So now I want to return the oldest date of each message_id. Here is the query I wrote:
SELECT `email_id`,
`message_id`,
MIN(`submit_dt`)
FROM `john_submissions`
WHERE `email_id` = '10'
GROUP BY `email_id`,`message_id`,`submit_dt`
But the results are exactly the same as above.
I was hoping to get results that looked like this:
| email_id | message_id | submit_dt |
-----------------------------------------------
| 10 | 1234 | 2025-01-23 13:30:01 |
-----------------------------------------------
| 10 | 2345 | 2025-02-08 13:00:02 |
-----------------------------------------------
How can I re-write my GROUP BY
query so that I get the above results? Or is there another way to do this?
I am trying to get the oldest date while using a GROUP BY
.
Using MySQL 5.7, I have a table called submissions. I can write a query the returns data like this:
SELECT `email_id`,
`message_id`,
`submit_dt`
FROM `submissions`
WHERE `email_id` = '10';
The above query returns 3 records that look like this:
| email_id | message_id | submit_dt |
-----------------------------------------------
| 10 | 1234 | 2025-01-23 13:30:01 |
-----------------------------------------------
| 10 | 1234 | 2025-01-23 14:00:01 |
-----------------------------------------------
| 10 | 2345 | 2025-02-08 13:00:02 |
-----------------------------------------------
So now I want to return the oldest date of each message_id. Here is the query I wrote:
SELECT `email_id`,
`message_id`,
MIN(`submit_dt`)
FROM `john_submissions`
WHERE `email_id` = '10'
GROUP BY `email_id`,`message_id`,`submit_dt`
But the results are exactly the same as above.
I was hoping to get results that looked like this:
| email_id | message_id | submit_dt |
-----------------------------------------------
| 10 | 1234 | 2025-01-23 13:30:01 |
-----------------------------------------------
| 10 | 2345 | 2025-02-08 13:00:02 |
-----------------------------------------------
How can I re-write my GROUP BY
query so that I get the above results? Or is there another way to do this?
1 Answer
Reset to default -2add in your SQL:
ORDER BY `submit_dt` ASC
本文标签: mysqlgroup by emailid then get oldest dateStack Overflow
版权声明:本文标题:mysql - group by email_id then get oldest date - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741241302a2364032.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
GROUP BY
list. That creates a different group for each date. – Barmar Commented Feb 24 at 21:23message_id
from theGROUP BY
, but I received an error that reads "Expression #2 SELECT list is not in GROUP BY clause and contains nonaggregated column 'db.submissions.message_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by". – John Beasley Commented Feb 24 at 21:31submit_dt
, notmessage_id
. – Barmar Commented Feb 24 at 21:34