日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql case when then 函数_MySQL case when then 语句使用和时间函数使用

發(fā)布時(shí)間:2023/12/20 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql case when then 函数_MySQL case when then 语句使用和时间函数使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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;

返回部分值

返回年、季度、月、日、時(shí)、分、秒、年月日、時(shí)分秒

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

跟周有關(guān)的函數(shù)

SELECTcreate_time,week(create_time)-- 返回所在周數(shù)(本年遇到的第一個(gè)星期天作為第一周,前面的計(jì)算為第0周),week(create_time,1)-- 返回所在周數(shù)(第一周超過3天,那么計(jì)算為本年的第一周),weekofyear(create_time)-- 返回所在周數(shù),yearweek(create_time)-- 返回年份和周數(shù),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')-- 返回年月日時(shí)分秒,date_format(create_time,'%w')-- 返回所在工作日索引值(0代表周日),time_format(create_time,'%H:%i')-- 返回時(shí)分FROMtime_function;

當(dāng)前時(shí)間和日期???????

SELECTcurrent_date()-- 當(dāng)前日期,current_time-- 當(dāng)前時(shí)間,now()-- 當(dāng)前時(shí)間和日期,localtime()-- 當(dāng)前時(shí)間和日期

時(shí)間和日期的計(jì)算

時(shí)間和日期的加減

固定時(shí)間類型的加減

函數(shù)說明

addtime()、subtime()時(shí)間的加減

adddate()、subdate()天數(shù)的加減

period_add()月份的加減

SELECTcreate_time,addtime(create_time,'1:00:00') as add1hour-- 增加一小時(shí),subtime(create_time,'1:00:00') as reduce1hour-- 減少一小時(shí),adddate(create_time,1) as add1day-- 增加一天,subdate(create_time,1) as reduce1day-- 減少一天,period_add(date_format(create_time,'%Y%m'),1) as add1month-- 增加一個(gè)月,period_add(date_format(create_time,'%Y%m'),-1) as reduce1month-- 減少一個(gè)月from time_function

自定義時(shí)間類型的加減

函數(shù)說明

timestampadd(type,int_expr,datetime)type:year,quarter,month,day,hour,minute,second

int_expr:所要加減的數(shù)值

datetime:所要處理的時(shí)間

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-- 增加一小時(shí),date_add(create_time,interval -1 hour) as reduce1hour_1-- 減少一小時(shí),date_sub(create_time,interval -1 hour) as add1hour_2-- 增加一小時(shí),date_sub(create_time,interval 1 hour) as reduce1hour_2-- 減少一小時(shí)FROMtime_function;

返回兩個(gè)時(shí)間格式之間的差值

返回固定類型的時(shí)間差

函數(shù)說明備注

timediff(datetime1,datetime2)返回兩者相差的時(shí)間其結(jié)果被限制在-838:59:59到838:59:59

datediff(datetime1,datetime2)返回兩者之間相差的天數(shù)無

period_diff(P1,P2)返回兩者之間相差的月份格式均為YYYYMM或者YYMM

SELECTcreate_time,timediff('2019-03-01 16:00:00',create_time) as date_diff-- 返回兩者之間相差的時(shí)間,now()-- 當(dāng)前時(shí)間,datediff(now(),create_time) as date_diff-- 返回兩者之間相差的天數(shù),date_format(now(),'%Y%m')-- 當(dāng)前月份,period_diff(date_format(now(),'%Y%m'),date_format(create_time,'%Y%m')) as month_diff-- 返回兩者之間的月份差值FROMtime_function;

返回自定義類型的時(shí)間差

TIMESTAMPDIFF(type,datetime1,datetime2)???????

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

來源:https://www.icode9.com/content-2-849751.html

總結(jié)

以上是生活随笔為你收集整理的mysql case when then 函数_MySQL case when then 语句使用和时间函数使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。