SQL嵌套查询总结
SQL嵌套查询总结
发布时间:2016-12-28 来源:查字典编辑
摘要: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 ";

分享给大家.

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新数据库其他学习
热门数据库其他学习
编程开发子分类