我如何才能获得每小时的第一排?

时间:2017-08-14 14:46:02

标签: mysql

MySQL []>从阅读中显示列;

| Field      | Type       | Null | Key | Default | Extra          |
| cont       | int(4)     | NO   | PRI | NULL    | auto_increment |
| id         | int(4)     | YES  | MUL | NULL    |                |
| sensor_num | int(4)     | YES  |     | NULL    |                |
| value      | float(5,2) | YES  |     | NULL    |                |
| time       | datetime   | YES  |     | NULL    |                |

从阅读中选择*;

  18570 |    1 |          1 | 23.93 | 2017-08-14 12:45:01 |
| 18571 |    1 |          1 | 23.93 | 2017-08-14 12:46:01 |
| 18572 |    1 |          1 | 23.93 | 2017-08-14 12:47:01 |
| 18573 |    1 |          1 | 23.93 | 2017-08-14 12:48:01 |
| 18574 |    1 |          1 | 23.93 | 2017-08-14 12:49:02 |
| 18575 |    1 |          1 | 23.93 | 2017-08-14 12:50:02 |
| 18576 |    1 |          1 | 23.93 | 2017-08-14 12:51:02 |
| 18577 |    1 |          1 | 23.93 | 2017-08-14 12:52:02 |
| 18578 |    1 |          1 | 23.93 | 2017-08-14 12:53:02 |
| 18579 |    1 |          1 | 23.93 | 2017-08-14 12:54:02 |
| 18580 |    1 |          1 | 23.93 | 2017-08-14 12:55:02 |
| 18581 |    1 |          1 | 23.93 | 2017-08-14 12:56:02 |
| 18582 |    1 |          1 | 23.93 | 2017-08-14 12:57:02 |

| 18689 |    1 |          1 | 25.88 | 2017-08-14 14:54:56 |
| 18690 |    1 |          1 | 25.88 | 2017-08-14 14:55:56 |
| 18691 |    1 |          1 | 25.88 | 2017-08-14 14:56:56 |
| 18692 |    1 |          1 | 25.88 | 2017-08-14 14:57:56 |
| 18693 |    1 |          1 | 25.88 | 2017-08-14 14:58:56 |
| 18694 |    1 |          1 | 25.88 | 2017-08-14 14:59:56 |
| 18695 |    1 |          1 | 25.88 | 2017-08-14 15:00:56 |
| 18696 |    1 |          1 | 25.88 | 2017-08-14 15:01:56 |
| 18697 |    1 |          1 | 25.88 | 2017-08-14 15:02:56 |
| 18698 |    1 |          1 | 25.88 | 2017-08-14 15:03:56 |
| 18699 |    1 |          1 | 25.88 | 2017-08-14 15:04:56 |
| 18700 |    1 |          1 | 25.88 | 2017-08-14 15:05:56 |

1 个答案:

答案 0 :(得分:0)

如果您想要每小时的第一行,您可以按小时选择与最小(时间)组匹配的行:

  select * 
  from reading a
  inner join (
    select date(time) t_date, min(time) t_time
    from reading 
    group by date(time), hour(time)
  ) t on  t.t_time = a.time