首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一个筛选有关问题

2013-09-07 
一个筛选问题有表如下:field1field2field3張三5A張三B張三56C張三4A李四8T李四5E李四2李四3E李四4Y李四7Y

一个筛选问题
有表如下:
field1field2field3
張三5A
張三B
張三56C
張三4A
李四8T
李四5E
李四2
李四3E
李四4Y
李四7Y
王五8T
王五8H
王五4G
趙六2J
趙六34E
趙六5Y
趙六6U
趙六4H
趙六8B
趙六7F
趙六8R
请问如何可以选出field2,field3都不是空的人,并且distinct出来人名?
[解决办法]

create table #tb(field1 varchar(10),field2 varchar(10),field3 varchar(10))
insert into #tb
select '張三','5','A'
union all select '張三',null,'B'
union all select '張三',56,'C'
union all select '張三',4,'A'
union all select '李四',8,'T'
union all select '李四',5,'E'
union all select '李四',2,null
union all select '李四',3,'E'
union all select '李四',4,'Y'
union all select '李四',7,'Y'
union all select '王五',8,'T'
union all select '王五',8,'H'
union all select '王五',4,'G'
union all select '趙六',2,'J'
union all select '趙六',34,'E'
union all select '趙六',5,'Y'
union all select '趙六',6,'U'
union all select '趙六',4,'H'
union all select '趙六',8,'B'
union all select '趙六',7,'F'
union all select '趙六',8,'R'

select distinct field1
from #tb 
where field1 not in(select field1 from #tb where isnull(field1,'')='' or isnull(field2,'')='')

drop table #tb


/*
李四
王五
趙六

*/

热点排行
Bad Request.