時間字段作為查詢條件

2021-09-19 19:15:00 字數 2905 閱讀 9097

//時間戳

select order_no,process_type from doms_order_master_201901 where date(order_date)="2019-1-23"

// 可以選擇 between and

select * from market_orders where createdate>='2010-03-01 00:00:00' and createdate<'2016-03-02 00:00:00'

-- 查詢指定時間段的資料

select fullname,addedtime from t_user where addedtime between '2017-1-1 00:00:00' and '2018-1-1 00:00:00';

select fullname,addedtime from t_user where addedtime >='2017-1-1 00:00:00' and addedtime < '2018-1-1 00:00:00';

//更新當前時間

update doms_order_master_201904 set order_date=date_format(now(),'%y-%m-%d %h:%m:%s') where order_date< date_format(now(),'%y-%m-%d %h:%m:%s')

-- 今天

select fullname,addedtime from user where to_days(addedtime) = to_days(now());

-- 昨天

select fullname,addedtime from user where to_days(now()) - to_days(addedtime) <= 1;

-- 近7天

select fullname,addedtime from user where date_sub(curdate(),interval 7 day) <= date(addedtime);

-- 近30天

select fullname,addedtime from user where date_sub(curdate(), interval 30 day) <= date(addedtime);

-- 本月

select fullname,addedtime from user where date_format( addedtime, '%y%m' ) = date_format( curdate() , '%y%m' );

-- 上一月

select fullname,addedtime from user where period_diff( date_format( now( ) , '%y%m' ) , date_format( addedtime, '%y%m' ) ) =1;

-- 查詢本季度資料

select fullname,addedtime from user where quarter(addedtime)=quarter(now());

-- 查詢上季度資料

select fullname,addedtime from user where quarter(addedtime)=quarter(date_sub(now(),interval 1 quarter));

-- 查詢本年資料

select fullname,addedtime from user where year(addedtime)=year(now());

-- 查詢上年資料

select fullname,addedtime from user where year(addedtime)=year(date_sub(now(),interval 1 year));

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

select fullname,addedtime from user where addedtime between date_sub(now(),interval 6 month) and now();

-- 查詢當前這週的資料

select fullname,addedtime from user where yearweek(date_format(addedtime,'%y-%m-%d')) = yearweek(now());

-- 查詢上週的資料

select fullname,addedtime from user where yearweek(date_format(addedtime,'%y-%m-%d')) = yearweek(now())-1;

-- 查詢上個月的資料

select fullname,addedtime from user where date_format(addedtime,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m');

-- 查詢當前月份的資料

select fullname,addedtime from user where date_format(addedtime,'%y%m') = date_format(curdate(),'%y%m');

select fullname,addedtime from user where date_format(addedtime,'%y-%m')=date_format(now(),'%y-%m');

-- 查詢指定時間段的資料 都是可以去掉天以後的

select fullname,addedtime from user where addedtime between '2017-1-1 00:00:00' and '2018-1-1 00:00:00';

select fullname,addedtime from user where addedtime >='2017-1-1 00:00:00' and addedtime < '2018-1-1 00:00:00';

Oracle中時間字段作為篩選條件

我們知道oracle資料庫中存的日期字段可能有好幾種寫法,這樣我們在作為篩選條件的時候就會無從下手,這時候我們可以用乙個函式 to char 幫我們搞定,我們以employees表為例 可以這樣寫 select employee id,first name hire date from employ...

Django查詢條件使用變數作為欄位名

value value field field name testmodel.objects.get 踩坑是因為專案中有數個表,每個表都有乙個欄位是外來鍵字段,例如各表都有 carfield,busfield,trainfield,bikefield,每個表除了外來鍵字段以外其他欄位都相同 現在想寫...

MySQL如何將字段內容作為查詢條件

例如 查詢task表中task progress欄位為8,並且建立時間距離當前時間的天數大於arragement valid days欄位中的數字天數。那麼就可以使用contact 函式直接取出字段內容。select r.from t task r where r.task progress 8an...