MySQL case when 使用

根据 case when 新的 sort字段排序

    case 
    when t2.status = 4
    and t2.expire_time>UNIX_TIMESTAMP()
    and t2.expire_time<UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL 60 day)) then 4
        when `status` = 2  then 3
        when `status` = 3 then 2
        when t2.status = 4
        and t2.expire_time>UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL 60 day))
        and t2.expire_time<UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL 1 year)) then 1
    else 0
    end sort     
    
     $query->orderBy('sort desc ,t2.expire_time desc,t2.created_at desc');
  • 当colume 与condition 条件相等时结果为result
  • case colume 
        when condition then result
        when condition then result
        when condition then result
    else result
    end

     

  • 当满足某一条件时,执行某一result
case  
    when condition then result
    when condition then result
    when condition then result
else result
end
  • 当满足某一条件时,执行某一result,把该结果赋值到new_column_name 字段中
  • case  
        when condition then result
        when condition then result
        when condition then result
    else result
    end new_column_name

     

 

源文:

https://www.cnblogs.com/xp796/p/8042100.html

 

posted @ 2019-10-16 06:15  BloggerSb  阅读(126)  评论(0编辑  收藏  举报