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

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
) A
where number1+number2+number3+number4+number5<>0