MySQL case when 使用

MySQL case when 使用

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

    image

  • 当满足某一条件时,执行某一result
    ```

case
when condition then result
when condition then result
when condition then result
else result
end

![image](//note.youdao.com/yws/res/40025/WEBRESOURCE6489287fb0198b71decf1df9891ccac4)


- 当满足某一条件时,执行某一result,把该结果赋值到new_column_name 字段中

case
when condition then result
when condition then result
when condition then result
else result
end new_column_name
```

阅读更多
文章标签: Mysql
个人分类: Mysql
上一篇Mysql将查询后的数据进行排名的SQL语句
下一篇MySql存储引擎Innodb和MyISAM的区别
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭