MySQLcasewhenthen函数使用和时间函数操作

原创
小哥 3年前 (2022-11-02) 阅读数 85 #PHP
文章标签 Laravel

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 -- 时间

跟周有关的函数

SELECT 

所属第几天

SELECT 

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

SELECT 

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

SELECT 

当前时间和日期​​​​​​​

SELECT 

时间和日期的计算

时间和日期的加减

固定时间类型的加减

函数

说明

addtime()、subtime()

时间的加减

adddate()、subdate()

天数的加减

period_add()

月份的加减

SELECT 

自定义时间类型的加减

函数

说明

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 

返回两个时间格式之间的差值

返回固定类型的时间差

函数

说明

备注

timediff(datetime1,datetime2)

返回两者相差的时间

其结果被限制在 -838:59:59838:59:59

datediff(datetime1,datetime2)

返回两者之间相差的天数

period_diff(P1,P2)

返回两者之间相差的月份

格式均为YYYYMM或者YYMM

SELECT 

返回自定义类型的时间差

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

SELECT create_time
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门