-3

我有一张桌子:订单,需要提出请求并获取其他桌子。我的数据库表:

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
...

我需要提出请求并获取此表(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)*/

谢谢!

如果我提出这个要求:

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

我会得到这张桌子:

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

3 回答 3

2

尝试:

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

工作示例:http ://sqlfiddle.com/#!2/e799a/1

于 2012-05-31T10:42:25.410 回答
1

试试这个,这样就可以了。

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;
于 2012-05-31T10:58:26.910 回答
0

也许这有帮助:

SELECT temp.`close`
FROM (
SELECT `close`
FROM `orders`
ORDER BY `close` DESC
) AS temp
ORDER BY `close` ASC
于 2012-05-31T11:14:26.913 回答