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 ";

分享给大家.
标签:
SQL,嵌套查询

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
千金楼资源网 Copyright www.htabc.com

评论“SQL嵌套查询总结”

暂无“SQL嵌套查询总结”评论...