MYSQL request | GROUP BY DAY [closed]

2019-09-22 02:46发布

I have a table: orders, and need to make a request and get other table. My DB table:

id  close
1   2012-05-29 03:11:15
2   2012-05-30 03:11:40
3   2012-05-31 03:12:10
4   2012-05-31 03:14:13
5   2012-05-31 03:16:50
6   2012-05-31 03:40:07     
7   2012-05-31 05:22:18
8   2012-05-31 05:22:22
9   2012-05-31 05:22:50
...

I need to make a request and get this table (GROUP BY DAY(close)):

1   2012-05-29 03:11:15
2   2012-05-30 03:11:40
9   2012-05-31 05:22:50 /*This is a last record on this day (05-31)*/

Thanks!

If I make this request:

SELECT id, close
FROM `orders`
GROUP BY DAY(close)
ORDER BY id ASC

I will get this table:

1   2012-05-29 03:11:15
2   2012-05-30 03:11:40
3   2012-05-31 03:12:10

3条回答
地球回转人心会变
2楼-- · 2019-09-22 03:22

Try:

select t1.* 
from orders t1
join (
    select max(close) as close
    from orders
    group by date(close)
) t2 on t1.close = t2.close

Working example: http://sqlfiddle.com/#!2/e799a/1

查看更多
唯我独甜
3楼-- · 2019-09-22 03:24

Maybe this help:

SELECT temp.`close`
FROM (
SELECT `close`
FROM `orders`
ORDER BY `close` DESC
) AS temp
ORDER BY `close` ASC
查看更多
家丑人穷心不美
4楼-- · 2019-09-22 03:27

Try this, this will do.

SELECT 
    a.id,
    a.close
FROM
(
    SELECT 
        id,
        close
    FROM
        `orders`
    ORDER BY
        close DESC
) AS a
GROUP BY 
    DATE(a.close)
ORDER BY 
    a.id
ASC;
查看更多
登录 后发表回答