说有点雷人,是我个人的感觉,可能高手认为小菜。
比如下表:
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='深圳'可否通过一个什么语句达到我想要的效果。

解决方案 »

  1.   

    where是选择。
    select是投影。
      

  2.   

    应该还要建立个NUM,PLACE地区表
      

  3.   

    select *,case num when '020' then '广州' 
     when '027' then '武汉' 
     when '0755' then '深圳' end as city from 表名 
      

  4.   

    select *,case num when '020' then '广州' 
     when '027' then '武汉' 
     when '0755' then '深圳' end as city from 表名 
      

  5.   

    最简单方法,
    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>
      

  6.   

    select IDID,NameName,CASE WHEN NameName='020' THEN '广州' WHEN NameName='027' THEN '武汉' WHEN NameName='0755' THEN '深圳' END AS City from #table 
      

  7.   

    完全可以.语句:
    select 
      *,
      case num 
        when '020' then '广州' 
        when '027' then '武汉' 
        when '0755' then '深圳' 
        when '0531' then '济南'
      end as city from 表名 
      

  8.   

    你再建立一个区号表,两表一关联就行了.不用写case when这么麻烦.
      

  9.   

    你说的只是举例了,你真正要显示的最后一行和之前一行有没关联的?如果有关联case起来就要简单些不用看上去那么累了