MySQL懶查詢 mysql查詢今天 昨天 上週

2021-10-22 07:51:32 字數 2192 閱讀 1289

今天

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' )

上一月select * from 表名 where period_diff( date_format( now( ) , '%y%m' ) , date_format( 時間欄位名, '%y%m' ) ) =1

#查詢本季度資料

select * from `ht_invoice_information` where quarter(create_date)=quarter(now());

#查詢上季度資料

select * from `ht_invoice_information` where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));

#查詢本年資料

select * from `ht_invoice_information` where year(create_date)=year(now());

#查詢上年資料

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查詢當前這週的資料

select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now());

查詢上週的資料

select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now())-1;

查詢當前月份的資料

select name,submittime from enterprise   where date_format(submittime,'%y-%m')=date_format(now(),'%y-%m')

查詢距離當前現在6個月的資料

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

查詢上個月的資料

select name,submittime from enterprise   where date_format(submittime,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m')

知識點:

mysql date 函式

下面的**列出了 mysql 中最重要的內建日期函式:

函式描述

返回當前的日期和時間 2008-12-29 16:25:46

返回當前的日期          2008-12-29

返回當前的時間          16:25:46

提取日期或日期/時間表示式的日期部分

返回日期/時間按的單獨部分

給日期新增指定的時間間隔

從日期減去指定的時間間隔

返回兩個日期之間的天數

用不同的格式顯示日期/時間

to_days(date)  :作用是給出乙個日期date,返回乙個天數(從公元0年的天數);

select orderid,date_sub(orderdate,interval 2 day) as orderpaydate

from orders

date_sub(curdate(), interval 30 day)

mysql刪除查詢 MySQL 刪除查詢

如果想從 mysql 表中刪除記錄,就要用到 sql 命令 delete from 可以在命令列中使用該命令,也可以在 php 指令碼中使用它。語法格式 下面是利用 delete 命令刪除 mysql 表中資料的一般語法格式 delete from table name where clause 如...

mysql 慢查詢 MySQL慢查詢

一 簡介 開啟慢查詢日誌,可以讓mysql記錄下查詢超過指定時間的語句,通過定位分析效能的瓶頸,才能更好的優化資料庫系統的效能。二 引數說明 slow query log 慢查詢開啟狀態 slow query log file 慢查詢日誌存放的位置 這個目錄需要mysql的執行帳號的可寫許可權,一般...

mysql模糊查詢 MYSQL模糊查詢

mysql提供標準的sql模式匹配,以及一種基於象unix實用程式如vi grep和sed的擴充套件正規表示式模式匹配的格式。一 sql模式 sql的模式匹配允許你使用 匹配任何單個字元,而 匹配任意數目字元 包括零個字元 在 mysql中,sql的模式預設是忽略大小寫的。下面顯示一些例子。注意在你...