mysql case when then 函数_MySQL case when then 语句使用和时间函数使用
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)-- 返回時分秒FROMtime_function;
跟周有關的函數
SELECTcreate_time,week(create_time)-- 返回所在周數(本年遇到的第一個星期天作為第一周,前面的計算為第0周),week(create_time,1)-- 返回所在周數(第一周超過3天,那么計算為本年的第一周),weekofyear(create_time)-- 返回所在周數,yearweek(create_time)-- 返回年份和周數,weekday(create_time)-- 返回工作日索引值(0代表周一),dayofweek(create_time)-- 返回工作日索引值(1代表周日)FROMtime_function;
所屬第幾天
SELECTcreate_time,dayofmonth(create_time)-- 返回月份的第幾天,dayofyear(create_time)-- 返回年份的第幾天FROMtime_function;
返回英文名???????
SELECTcreate_time,dayname(create_time)-- 返回工作日英文名,monthname(create_time)-- 返回月份英文名FROMtime_function;
返回特定的格式???????
SELECTcreate_time,date_format(create_time,'%Y-%m-%d %H:%i:%s')-- 返回年月日時分秒,date_format(create_time,'%w')-- 返回所在工作日索引值(0代表周日),time_format(create_time,'%H:%i')-- 返回時分FROMtime_function;
當前時間和日期???????
SELECTcurrent_date()-- 當前日期,current_time-- 當前時間,now()-- 當前時間和日期,localtime()-- 當前時間和日期
時間和日期的計算
時間和日期的加減
固定時間類型的加減
函數說明
addtime()、subtime()時間的加減
adddate()、subdate()天數的加減
period_add()月份的加減
SELECTcreate_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)
SELECTcreate_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-- 減少一小時FROMtime_function;
返回兩個時間格式之間的差值
返回固定類型的時間差
函數說明備注
timediff(datetime1,datetime2)返回兩者相差的時間其結果被限制在-838:59:59到838:59:59
datediff(datetime1,datetime2)返回兩者之間相差的天數無
period_diff(P1,P2)返回兩者之間相差的月份格式均為YYYYMM或者YYMM
SELECTcreate_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-- 返回兩者之間的月份差值FROMtime_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-- 返回兩者之間的天數差值FROMtime_function;
來源:https://www.icode9.com/content-2-849751.html
總結
以上是生活随笔為你收集整理的mysql case when then 函数_MySQL case when then 语句使用和时间函数使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql重做日志与binlog日志区别
- 下一篇: java 标记_java的标记算法