欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL嵌套查询总结

程序员文章站 2022-11-21 10:34:44
it也有一段时间了,刚开始的时候`````` 的困难主要是在编程语言上,数组,逻辑,算法,... 这些都过来了之后,困难就上升到数据库设计上了. 以及数据逻辑. 一个优秀的...
it也有一段时间了,刚开始的时候``````
的困难主要是在编程语言上,数组,逻辑,算法,...
这些都过来了之后,困难就上升到数据库设计上了.
以及数据逻辑.
一个优秀的系统,会集成优秀的程序和优秀的数据库设计.
要做到这点得有足够的经验.
这是我写的一个结合uinon的嵌套查询.
将五个方面的报表放到一个临时表里,再从临时表里,将所要的数据查询出来. 
复制代码 代码如下:

$sql="select type , sum( yjsl ) as yjsl , sum( yysl ) as yysl, sum( jyrs ) as jyrs, sum( jycs ) as jycs
from (
select c.mc as
type , count( d.lsh ) as yjsl, 0 as yysl, 0 as jyrs, 0 as jycs
from sys_dzxxb as b, sys_jcb as c, sys_dzyjb as d
where b.bm = c.lsh
and d.dzlsh = b.lsh
group by c.mc
union select c.mc as
type , 0 as yjsl, count( e.lsh ) as yysl, 0 as jyrs, 0 as jycs
from sys_dzxxb as b, sys_jcb as c, sys_dzyy as e
where b.bm = c.lsh
and e.dzlsh = b.lsh
group by c.mc
union select c.mc as
type , 0 as yjsl, 0 as yysl, count( distinct e.dzlsh ) as jyrs, 0 as jycs
from sys_dzxxb as b, sys_jcb as c, sys_ltxxb as e
where b.bm = c.lsh
and e.dzlsh = b.lsh
group by c.mc
union select c.mc as
type , 0 as yjsl, 0 as yysl, 0 as jyrs, count( distinct e.lsh ) as jycs
from sys_dzxxb as b, sys_jcb as c, sys_ltxxb as e
where b.bm = c.lsh
and e.dzlsh = b.lsh
group by c.mc
) as temptable
group by type ";

分享给大家.