日期:2014-05-18  浏览次数:20648 次

sql 语句 查询
表 A
 Id 1 2011 张三 手
 ID 2 2012 李四 脚 
 ID 3 2011 王五 眼
 id 4 2012 小名 眼 手

  我要的结果

 2011 张三 手 NULL NULL
 2011 王五 NULL NULL 眼
 2012 李四 NULL 脚 NULL
 2012 小名 手 NULL 眼

------解决方案--------------------
按楼上所说的
SQL code

CREATE TABLE BBBB
(
  idx int,
  yea int,
  name varchar(20),
  qiguan varchar(20) 
)

insert into BBBB 
select 1,2011,'张三','手' union all
select 2,2012,'李四','脚' union all   
select 3,2011,'王五','眼' union all
select 4,2012,'小名','眼手'

select yea,[name],shou=case when patindex(qiguan,'手')>0 then '手' else null end,
jiao=case when patindex(qiguan,'脚')>0 then '脚' else null end, 
yan=case when patindex(qiguan,'眼')>0 then '眼' else null end from BBBB

yea         name                 shou jiao yan  
----------- -------------------- ---- ---- ---- 
2011        张三                   手    NULL NULL
2012        李四                   NULL 脚    NULL
2011        王五                   NULL NULL 眼
2012        小名                   NULL NULL NULL

(所影响的行数为 4 行)