说有点雷人,是我个人的感觉,可能高手认为小菜。
比如下表:
ID num
1 020
2 027
3 0755我能不能通过一个SQL语句达到这样的输出效果。
ID num city
1 020 广州
2 027 武汉
3 0755 深圳比如这么写,(下面这句当然是错的)
select *,num as city from 表名
where num='020' then city='广州'
num='027' then city='武汉'
num='0755' then city='深圳'可否通过一个什么语句达到我想要的效果。
比如下表:
ID num
1 020
2 027
3 0755我能不能通过一个SQL语句达到这样的输出效果。
ID num city
1 020 广州
2 027 武汉
3 0755 深圳比如这么写,(下面这句当然是错的)
select *,num as city from 表名
where num='020' then city='广州'
num='027' then city='武汉'
num='0755' then city='深圳'可否通过一个什么语句达到我想要的效果。
select是投影。
when '027' then '武汉'
when '0755' then '深圳' end as city from 表名
when '027' then '武汉'
when '0755' then '深圳' end as city from 表名
1> select 'ID num city'
2> union all
3> select '1 020 广州'
4> union all
5> select '2 027 武汉'
6> union all
7> select '3 0755 深圳'
8> go--------------
ID num city
1 020 广州
2 027 武汉
3 0755 深圳(4 rows affected)
1>
select
*,
case num
when '020' then '广州'
when '027' then '武汉'
when '0755' then '深圳'
when '0531' then '济南'
end as city from 表名