Mysql查询结果日期时间明智

时间:2014-12-05 09:56:41

标签: mysql sql date select sql-order-by

我试过这样的 这是我的查询

SELECT sloat_day, sloat_start_time 
FROM time_table 
WHERE sloat_day BETWEEN '2014-12-01' AND '2014-12-07' 
ORDER BY sloat_day, sloat_start_time ASC

我得到了以下输出

sloat_day   sloat_start_time
2014-12-01  6.30
2014-12-01  7.30
2014-12-01  18.30
2014-12-01  19.30
2014-12-02  6.30
2014-12-02  7.30
2014-12-02  18.30
2014-12-02  19.30
2014-12-03  6.30
2014-12-03  7.30
2014-12-03  18.30
2014-12-03  19.30
2014-12-04  18.30
2014-12-04  19.30
2014-12-05  7.30
2014-12-05  18.30
2014-12-05  19.30
2014-12-06  19.30
2014-12-07  7.30
2014-12-07  8.30
2014-12-07  18.30
2014-12-07  19.30

我想在下面查询结果我想在查询中更改以获得此输出请帮助我

2014-12-01  6.30
2014-12-02  6.30
2014-12-03  6.30
2014-12-04  18.30
2014-12-05  7.30
2014-12-06  19.30
2014-12-07  7.30
2014-12-01  7.30
2014-12-02  7.30
2014-12-03  7.30
2014-12-04  19.30
2014-12-05  18.30
2014-12-07  8.30
2014-12-01  18.30
2014-12-02  18.30
2014-12-03  18.30
2014-12-05  19.30
2014-12-07  18.30

3 个答案:

答案 0 :(得分:1)

更改ORDER clausule中列的顺序,如下所示。

SELECT sloat_day, sloat_start_time 
FROM time_table 
WHERE sloat_day BETWEEN '2014-12-01' AND '2014-12-07' 
ORDER BY sloat_start_time, sloat_day ASC

这样,查询将首先由sloat_start_time命令。

答案 1 :(得分:0)

尝试运行此

SELECT sloat_day, sloat_start_time 
FROM time_table 
WHERE sloat_day BETWEEN '2014-12-01' AND '2014-12-07' 
GROUP BY sloat_day
ORDER BY sloat_start_time, sloat_day ASC

MySQL将按照您要求的顺序运行所有内容,因此首先按sloat_day设置顺序将首先按该列排序。

答案 2 :(得分:0)

试试这个:

SELECT sloat_day, sloat_start_time
FROM (SELECT sloat_day, sloat_start_time, 
             IF(@day=@day:=sloat_day, @id:=@id+1, @id:=1) rank  
      FROM time_table, (SELECT @id:=0, @day:='') AS a 
      WHERE sloat_day BETWEEN '2014-12-01' AND '2014-12-07' 
      ORDER BY sloat_day, sloat_start_time 
    ) AS A 
ORDER BY rank, sloat_day, sloat_start_time

检查SQL FIDDLE DEMO

<强>输出

|         SLOAT_DAY | SLOAT_START_TIME |
|-------------------| -----------------|
| December, 01 2014 | 06:30:00         |
| December, 02 2014 | 06:30:00         |
| December, 03 2014 | 06:30:00         |
| December, 04 2014 | 18:30:00         |
| December, 05 2014 | 07:30:00         |
| December, 06 2014 | 19:30:00         |
| December, 07 2014 | 07:30:00         |
| December, 01 2014 | 07:30:00         |
| December, 02 2014 | 07:30:00         |
| December, 03 2014 | 07:30:00         |
| December, 04 2014 | 19:30:00         |
| December, 05 2014 | 18:30:00         |
| December, 07 2014 | 08:30:00         |        
| December, 01 2014 | 18:30:00         |
| December, 02 2014 | 18:30:00         |
| December, 03 2014 | 18:30:00         |
| December, 05 2014 | 19:30:00         |
| December, 07 2014 | 18:30:00         |
| December, 01 2014 | 19:30:00         |
| December, 02 2014 | 19:30:00         |
| December, 03 2014 | 19:30:00         |
| December, 07 2014 | 19:30:00         |