【问题标题】:MySQL: selecting dates (from timestamp) for which condition (related to other fields in the row) is fulfilledMySQL:选择满足条件(与行中的其他字段相关)的日期(从时间戳)
【发布时间】:2022-01-27 13:23:21
【问题描述】:

我的 SQL 知识比较薄弱,我来自过程编程,所以请多多包涵。我有一个数据库,其中包含来自气象站的数据 - 这些数据每分钟收集一次,表的(重要部分)是

MariaDB [weather]> describe readings;
+------------------+------------+------+-----+-------------------+-------+
| Field            | Type       | Null | Key | Default           | Extra |
+------------------+------------+------+-----+-------------------+-------+
| time             | timestamp  | NO   | PRI | CURRENT_TIMESTAMP |       |
| inside_temp      | float      | YES  |     | NULL              |       |
| outside_temp     | float      | YES  |     | NULL              |       |
+------------------+------------+------+-----+-------------------+-------+

我想找出outside_temp 不低于或不大于某些值的所有日子。

我可以使用 MySQL 在外部对其进行编码,以进行类似的查询

select min(outside_temp), max(outside_temp) from readings where date(time)='2022-01-27';

并遍历数据库中的所有天以分别检查每天的温度值,但我想知道是否可以仅使用 MySQL 命令进行选择(我想是的,超出我的想象)。

【问题讨论】:

    标签: mysql timestamp iteration days


    【解决方案1】:

    像select date(time), min(outside_temp), max(outside_temp) from readings group by date(time); 这样的东西会给你所有满足要求的时间戳

    【讨论】:

    • 不,它列出了每天的最低/最高温度,而不是温度在给定范围内的日子(尽管这可能是一种创建临时表的方法,从中选择我感兴趣的日期in 将是微不足道的)
    • 真的,你可以这样做:SELECT * FROM (select date(time) as date, min(outside_temp) as outside_min, max(outside_temp) as outsidemax from readings group by date(time)) 其中outside_min > 1 and outside_max 1 and outsidemax
    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 2012-06-23
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 2021-12-23
    相关资源
    最近更新 更多