MySQL查询,关于日期和时间戳,查询时间的记录

编程入门 行业动态 更新时间:2024-10-23 21:26:13

MySQL查询,关于日期和<a href=https://www.elefans.com/category/jswz/34/1771441.html style=时间戳,查询时间的记录"/>

MySQL查询,关于日期和时间戳,查询时间的记录

一. 日期与时间戳
数据库日期写入大多数用的是时间戳格式,我们在查询的时候可能不是很方便
mysql提供了两个函数:

  1. from_unixtime(time_stamp) -> 将时间戳转换为日期
    mysql> select from_unixtime(create_time) from tag limit 10;
    +----------------------------+
    | from_unixtime(create_time) |
    +----------------------------+
    | 2017-03-15 08:30:46        |
    | 2017-03-15 10:30:06        |
    | 2017-03-14 17:06:42        |
    | 2017-03-12 09:31:31        |
    | 2017-03-15 05:08:37        |
    | 2017-03-05 06:54:21        |
    | 2017-03-14 17:18:20        |
    | 2017-03-15 09:01:26        |
    | 2017-03-01 02:37:46        |
    | 2017-02-28 05:02:27        |
    +----------------------------+
    10 rows in set (0.00 sec)
  2. unix_timestamp(date) -> 将指定的日期或者日期字符串转换为时间戳
    select * from tag  where create_time >= unix_timestamp('2018-10-26 00:00:01') and create_time <= unix_timestamp('2018-12-11 23:59:59') order by total  desc limit 10;

二. 查询近30天的数据
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

mysql> select * from tag where date_sub(curdate(),interval 45 day) <= from_unixtime(create_time) order by total desc limit 10;

mysql查询今天、昨天、7天、近30天、本月、上一月 数据:
.html

转载于:

更多推荐

MySQL查询,关于日期和时间戳,查询时间的记录

本文发布于:2023-06-28 19:58:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/932769.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   日期   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!