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' )
上一月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
mybatis的轉譯字元
< < 小於
> > 大於
& & 與
' ' 單引號
" " 雙引號
查詢結果判斷處理
case when isnull(a.***) then '未設定' when a.*** = 1 then '女' when a.*** = 0 then '男' end as "***"
將生日轉換成歲數
timestampdiff(year, birthday, curdate())
date型別比對使用年月日,進行like
create_date like concat(#,'%')
傳入字串轉成數字進行比對
select * from a where id= convert('123',signed);
獲取最接近的值 主要運用abs關鍵字
select price_top from o_discounts where abs(price - #) = ( select min(abs(price - #)) from o_discounts where del_flag = # and # >= price ) and del_flag = # and # >= price
查詢string串裡面內容是否包含傳入值,要用逗號隔開,3是傳入值,返回結果1:有相同,0:沒有相同
記錄一些常用的sql語句
select name,submittime from enterprise where yearweek date format submittime,y m d yearweek now select name,submittime from enterprise where yearweek ...
一些sql語句
一。在oracle中建表,怎麼實現id自動編號 1 建表 create table code test id int,name varchar2 20 2.建立序列 create sequence s country id increment by 1 start with 1 maxvalue 9...
一些Sql語句
case when xx then yy else zz 例 case when count is null then 0 else count 當count為空的時候賦值0,不為空則取原值 isnull express1,express2 例 isnull count,0 當count為空的時候則...