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

sql根据查询结果,去掉空值解决方法

2012-03-11 
sql根据查询结果,去掉空值select (select orgname from org as og where org.idog.id),(select count(*)

sql根据查询结果,去掉空值
select (select orgname from org as og where org.id=og.id),
(select count(*) from tb1 as t1 whree t1.id=org.id) as number1,
(select count(*) from tb2 as t2 whree t2.id=org.id) as number2,
(select count(*) from tb3 as t3 whree t3.id=org.id) as number3,
(select count(*) from tb4 as t4 whree t4.id=org.id) as number4,
(select count(*) from tb5 as t5 whree t5.id=org.id) as number5  
from org as org,tb as tb where org.id=tb.id  
group by org.name having count(*)>0

语句也就是这么个意思,现在是查出来了,可是有那么几行查询结果为0的也显示出来了,我不想显示全为0的这
一行记录
如下:
orgname ------number1-------number2-------number3-------number4------number5
aaaa-----------0--------------2--------------0-------------1------------0
bbbb-----------1--------------0--------------0-------------0------------0
cccc-----------0--------------0--------------0-------------0------------0
dddd-----------0--------------0--------------0-------------0------------0
eeee-----------0--------------1--------------0-------------1------------0

就是不取出number1,number2,number3,number4,number5全为0的这一行记录
求大师指明!!!!!



[解决办法]
select (select orgname from org as og where org.id=og.id),
(select count(*) from tb1 as t1 whree t1.id=org.id) as number1,
(select count(*) from tb2 as t2 whree t2.id=org.id) as number2,
(select count(*) from tb3 as t3 whree t3.id=org.id) as number3,
(select count(*) from tb4 as t4 whree t4.id=org.id) as number4,
(select count(*) from tb5 as t5 whree t5.id=org.id) as number5
from org as org,tb as tb where org.id=tb.id 
and ( 
(select count(*) from tb1 as t1 whree t1.id=org.id)>0 or (select count(*) from tb2 as t2 whree t2.id=org.id)>0 or ...)
group by org.name having count(*)>0
[解决办法]
行不行楼主试一下再说
[解决办法]

SQL code
select * from (select (select orgname from org as og where org.id=og.id),(select count(*) from tb1 as t1 whree t1.id=org.id) as number1,(select count(*) from tb2 as t2 whree t2.id=org.id) as number2,(select count(*) from tb3 as t3 whree t3.id=org.id) as number3,(select count(*) from tb4 as t4 whree t4.id=org.id) as number4,(select count(*) from tb5 as t5 whree t5.id=org.id) as number5   from org as org,tb as tb where org.id=tb.id   group by org.name having count(*)>0) Awhere number1+number2+number3+number4+number5<>0 

热点排行