美文网首页我爱编程
MySQL 日期加减天数,Date比较

MySQL 日期加减天数,Date比较

作者: Kaidi_G | 来源:发表于2018-06-21 15:55 被阅读6次

    给出日期-天气表,求所有比前一天温度高的日期的Id.

    +---------+------------------+------------------+
    | Id(INT) | RecordDate(DATE) | Temperature(INT) |
    +---------+------------------+------------------+
    |       1 |       2015-01-01 |               10 |
    |       2 |       2015-01-02 |               25 |
    |       3 |       2015-01-03 |               20 |
    |       4 |       2015-01-04 |               30 |
    +---------+------------------+------------------+
    

    解法

    select a.Id from Weather a, weather b
    where a.RecordDate = DATE_ADD(b.RecordDate, INTERVAL 1 DAY)
    AND a.Temperature > b.Temperature
    

    select date_add(now(), interval 1 day); - 加1天
    select date_add(now(), interval 1 hour); -加1小时
    select date_add(now(), interval 1 minute); - 加1分钟
    select date_add(now(), interval 1 second); -加1秒
    select date_add(now(), interval 1 microsecond);-加1毫秒
    select date_add(now(), interval 1 week);-加1周
    select date_add(now(), interval 1 month);-加1月
    select date_add(now(), interval 1 quarter);-加1季
    select date_add(now(), interval 1 year);-加1年

    相关文章

      网友评论

      • 孙沛2010:hi,代码的背景怎么设置的,谢谢
        孙沛2010:@LSD_Monkey 谢了
        Kaidi_G:markdown 语法,https://www.jianshu.com/p/q81RER

      本文标题:MySQL 日期加减天数,Date比较

      本文链接:https://www.haomeiwen.com/subject/taxcyftx.html