MYSQL查詢今天 昨天 7天前 30天 本月資料

2021-09-11 11:19:24 字數 543 閱讀 6580

部落格引用處(以下內容在原有部落格基礎上進行補充或更改,謝謝這些大牛的部落格指導):

mysql查詢今天、昨天、7天前、30天、本月資料

今天:

select * from 表名 where to_days( 時間欄位名) = to_days(now());

昨天:select * from 表名 where to_days( now( ) ) - to_days( 時間欄位名) <= 1;

7天前:

select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間欄位名);

30天:

select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名);

本月:select * from 表名 where date_format( 時間欄位名, '%y%m' ) = date_format( curdate( ) , '%y%m' );

MYSQL查詢今天 昨天 7天前 30天 本月資料

mysql查詢今天 昨天 7天前 30天 本月資料 今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1 7天前 select from 表名 whe...

mysql獲取今天,昨天,前7天,前30天的資料

在專案中我們經常遇到需要對資料統計,就比如幾天前的,乙個月前,半年前等等。在mysql中為我們提供了日期處理的函式可以大大提高我們自己寫sql統計的效率。以下我會列舉4個統計日期的sql 查詢今天的資料 to days 日期字段 to days now 查詢昨天的資料 to days now to ...

mysql查詢今天 昨天 7天 近30天 本月資料

今天 select from 表名 where to days 時間欄位名 to days now 昨天select from 表名 where to days now to days 時間欄位名 1 datediff now from unixtime 時間欄位名 1 時間字段 儲存型別為時間戳 ...