mysql,SQL語句對於時間的查詢

2021-09-19 17:15:28 字數 2557 閱讀 3514

今天

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

select * from ` user ` where date_format(pudate, ' %y%m ' ) = date_format(curdate(), ' %y%m ' ) ;

select * from user where weekofyear(from_unixtime(pudate,'%y-%m-%d')) = weekofyear(now())

select * 

from user 

where month (from_unixtime(pudate, ' %y-%m-%d ' )) = month (now())

select * 

from [ user ] 

where year (from_unixtime(pudate, ' %y-%m-%d ' )) = year (now())

and month (from_unixtime(pudate, ' %y-%m-%d ' )) = month (now())

select * 

from [ user ] 

where pudate between 上月最後一天

and 下月第一天

where   date(regdate)   =   curdate();

select   *   from   test   where   year(regdate)=year(now())   and   month(regdate)=month(now())   and   day(regdate)=day(now())

select date( c_instime ) ,curdate( )

from `t_score`

where 1

limit 0 , 30

MySql Sql語句總結

建表語句 create table class id int primary key,class char 255 name varchar 4000 hobby text int 和 integer 是一樣的,只是為了簡寫罷了,主鍵宣告直接跟在定義後面,char和varchar char是固定長度...

MySQL SQL語句優化

檢視表定義 show create table users 檢視表的索引 show index from users 你要獲取第乙個表的所有資訊,你說全表掃瞄快呢還是索引掃瞄快呢?所以當你查詢庫 包括left join中的臨時庫 的所有資訊時,資料庫會選擇最優方法 全表掃瞄!s表dept id na...

MySQL sql語句筆記

sql 插入insert into 表名 列名 values 值列表 關鍵字 可省略 insert into stu name,age,city values 18,1 更新 update 表名 set 列名 更新值 where 更新條件 update stu set name sdfsdf age...