日期:2014-05-19  浏览次数:20459 次

如何实现映射
表   a  
列   id   ,   type   (只能取值1,2,3)   分别代表甲级,乙级,丙级.

如何写个查询语句
    结果如下显示如下:
        1       甲级
        2       乙级
        3       甲级
      ..   ...

------解决方案--------------------

create table 表a(id int , type varchar(10))
insert 表a
select 1, '1,2,3 '


select * from (select 1 id , '甲级 ' type from 表a where charindex( '1 ',type)> 0
union all
select 2 id , '乙级 ' type from 表a where charindex( '2 ',type)> 0
union all
select 3 id , '丙级 ' type from 表a where charindex( '3 ',type)> 0) a
order by id
------解决方案--------------------

create table T(id int, type int)
insert T select 1, 1
insert T select 2, 2
insert T select 3, 3

select id, type=case type when 1 then '甲级 ' when 2 then '乙级 ' when 3 then '丙级 ' end
from T

--result
id type
----------- ----
1 甲级
2 乙级
3 丙级

(3 row(s) affected)
------解决方案--------------------
如果是变量值:
方法相同:
declare @i int
set @i=1--定义2,3........
select case @i when 1 then '甲级 '
when 2 then '乙级 '
when 3 then '丙级 '
else ' ' end--不在范围内不显示