mysql中case、when、if、then的用法

版权声明:本文为博主原创文章,未经博主允许不得转载。有问题请联系Q:346660118 https://blog.csdn.net/Dreamhai/article/details/82152950

概述:

sql语句中的case语句与高级语言中的switch语句,是标准sql的语法,适用于一个条件判断有多种值的情况下分别执行不同的操作。

首先,让我们看一下CASE的语法。在一般的SELECT中,其语法格式如下:

 CASE  <单值表达式>

       WHEN <表达式值> THEN <SQL语句或者返回值>

       WHEN <表达式值> THEN <SQL语句或者返回值>

       ...

       WHEN <表达式值> THEN <SQL语句或者返回值>

       ELSE <SQL语句或者返回值>

 END

select 
  case a.position
  when '0' then '未知' when '1' then '首屏' when '3' then '首屏以下'
  when '4' then '页面顶部' when '5' then '页面底部' when '6' then '侧边栏'
  when '7' then '全屏' when '8' then '二屏' when '9' then '三屏'
  when '10' then '四屏' when '11' then '五屏' when '12' then '五屏以下'
  end as pp,
  case a.type
  when '0' then 'Banner'
  when '1' then 'Video'
  end as tt,
  case a.DeviceType
  when '0' then 'phone' when '1' then 'tablet' when '2' then 'pc' when '3' then 'TV'
  end as ttt
  from publisher p,adplacement a
  where a.PublisherId = p.id
  into outfile '/tmp/ad.csv' fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';

猜你喜欢

转载自blog.csdn.net/Dreamhai/article/details/82152950