1. 程式人生 > >MySQL查詢,關於日期和時間戳,查詢時間的記錄

MySQL查詢,關於日期和時間戳,查詢時間的記錄

total rom time logs date_sub 關於 我們 tps times

一. 日期與時間戳
數據庫日期寫入大多數用的是時間戳格式,我們在查詢的時候可能不是很方便
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天、本月、上一月 數據:
https://www.cnblogs.com/qinweizhi/p/5918048.html

MySQL查詢,關於日期和時間戳,查詢時間的記錄