1. 程式人生 > 其它 >MySQL case when then 語句使用和時間函式使用

MySQL case when then 語句使用和時間函式使用

技術標籤:Mysqlphp

Laravel上使用:

$list = Article::where('status',2)->where('category_id',$category_id)
->select(DB::raw('id, type,thumb_img,title,tags,intro,video_id,external_link
,live_start_time,live_end_time,live_id,page_views,zan_num,published_at,collection_num, case when collection_num=0 then timestampdiff(second,now(),live_start_time) else timestampdiff(second,live_start_time,now()) end as sort_time'))

->orderBy('collection_num','asc')
->orderBy('sort_time','asc')
->paginate($per_page);

原生MySQL語句:

SELECT id, type,thumb_img,title,tags,intro,video_id,external_link,live_start_time,live_end_time,live_id,page_views,zan_num,published_at,status , case when status =0 then timestampdiff(second,now(),live_start_time) else timestampdiff(second,live_start_time,now()) end as sort_time FROM `articles` WHERE category_id=4 ORDER BY status asc,sort_time asc;

返回部分值

返回年、季度、月、日、時、分、秒、年月日、時分秒

SELECT create_time -- 時間    ,YEAR(create_time)-- 返回年    ,QUARTER(create_time)-- 返回季度    , MONTH(create_time)-- 返回月    , DAY(create_time)-- 返回日    , HOUR(create_time)-- 返回小時    , MINUTE(create_time)-- 返回分鐘    , SECOND(create_time)-- 返回秒    , DATE(create_time)-- 返回年月日    , TIME(create_time)-- 返回時分秒
FROM time_function;

跟周有關的函式

SELECT     create_time     ,week(create_time)-- 返回所在週數(本年遇到的第一個星期天作為第一週,前面的計算為第0周)    ,week(create_time,1)-- 返回所在週數(第一週超過3天,那麼計算為本年的第一週)    ,weekofyear(create_time)-- 返回所在週數    ,yearweek(create_time)-- 返回年份和週數    ,weekday(create_time)-- 返回工作日索引值(0代表週一)    ,dayofweek(create_time)-- 返回工作日索引值(1代表週日)FROM    time_function;

所屬第幾天

SELECT     create_time     ,dayofmonth(create_time)-- 返回月份的第幾天    ,dayofyear(create_time)-- 返回年份的第幾天FROM    time_function;

返回英文名​​​​​​​

SELECT     create_time     ,dayname(create_time)-- 返回工作日英文名    ,monthname(create_time)-- 返回月份英文名FROM    time_function;

返回特定的格式​​​​​​​

SELECT     create_time     ,date_format(create_time,'%Y-%m-%d %H:%i:%s')-- 返回年月日時分秒    ,date_format(create_time,'%w')-- 返回所在工作日索引值(0代表週日)    ,time_format(create_time,'%H:%i')-- 返回時分FROM    time_function;

當前時間和日期​​​​​​​

SELECT current_date()-- 當前日期,current_time-- 當前時間,now()-- 當前時間和日期,localtime()-- 當前時間和日期

時間和日期的計算

時間和日期的加減

固定時間型別的加減

函式說明
addtime()、subtime()時間的加減
adddate()、subdate()天數的加減
period_add()月份的加減
SELECT create_time,addtime(create_time,'1:00:00') as add1hour-- 增加一小時,subtime(create_time,'1:00:00') as reduce1hour-- 減少一小時,adddate(create_time,1) as add1day-- 增加一天,subdate(create_time,1) as reduce1day-- 減少一天,period_add(date_format(create_time,'%Y%m'),1) as add1month-- 增加一個月,period_add(date_format(create_time,'%Y%m'),-1) as reduce1month-- 減少一個月from time_function

自定義時間型別的加減

函式說明
timestampadd(type,int_expr,datetime)

type:year,quarter,month,day,hour,minute,second

int_expr:所要加減的數值

datetime:所要處理的時間

date_add(datetime,interval int_expr type)
date_sub(datetime,interval int_expr type)
SELECT     create_time    ,TIMESTAMPADD(day,1,create_time) as add1day-- 增加一天    ,TIMESTAMPADD(day,-1,create_time) as reduce1day-- 減少一天    ,date_add(create_time,interval 1 hour) as add1hour_1-- 增加一小時    ,date_add(create_time,interval -1 hour) as reduce1hour_1-- 減少一小時    ,date_sub(create_time,interval -1 hour) as add1hour_2-- 增加一小時    ,date_sub(create_time,interval 1 hour) as reduce1hour_2-- 減少一小時FROM    time_function;

返回兩個時間格式之間的差值

返回固定型別的時間差

函式說明備註
timediff(datetime1,datetime2)返回兩者相差的時間其結果被限制在-838:59:59838:59:59
datediff(datetime1,datetime2)返回兩者之間相差的天數
period_diff(P1,P2)返回兩者之間相差的月份格式均為YYYYMM或者YYMM
SELECT     create_time    ,timediff('2019-03-01 16:00:00',create_time) as date_diff-- 返回兩者之間相差的時間    ,now()-- 當前時間    ,datediff(now(),create_time) as date_diff-- 返回兩者之間相差的天數    ,date_format(now(),'%Y%m')-- 當前月份    ,period_diff(date_format(now(),'%Y%m'),date_format(create_time,'%Y%m')) as month_diff-- 返回兩者之間的月份差值FROM    time_function;

返回自定義型別的時間差

TIMESTAMPDIFF(type,datetime1,datetime2)​​​​​​​

SELECT create_time    ,now()    ,timestampdiff(month,create_time,now()) as month_diff-- 返回兩者之間的月份差值    ,timestampdiff(day,create_time,now()) as day_diff-- 返回兩者之間的天數差值FROM    time_function;