自己是用的oralce,自己查了下mysql应该也是有的,其他需要自己验证
1.前端时间遇到一个查询需求:从四张表中分别查询: 年数据量和月数据量,然后通过统计图进行展示
2.第一次写的时候,没有想到好的方法去进行关联,因此采用了笨重的8次查询,虽然结果出来了,但显得很笨重,并且严重影响了首页的加载速度。
废话不多说直接上代码
主要 框架就是 select ( selet 参数 from 表 where 条件)别名,
( selet 参数 from 表 where 条件)别名,
( selet 参数 from 表 where 条件)别名
from dual
select
(select count(参数) from 表1 where to_char(SLSj ,'yyyy-MM') = to_char(sysdate,'yyyy-MM')) 别名1,
(select count(参数) from 表2 where to_char(SLSj ,'yyyy-MM') = to_char(sysdate,'yyyy-MM')) 别名2,
(select count(参数) from 表3 where to_char(SLRQ ,'yyyy-MM') = to_char(sysdate,'yyyy-MM')) 别名3,
(select count(参数) from 表4 where to_char(SLSj ,'yyyy-MM') = to_char(sysdate,'yyyy-MM')) 别名4,
(select count(参数) from 表1 where to_char(SLSj ,'yyyy') = to_char(sysdate,'yyyy')) 别名5,
(select count(参数) from 表2 where to_char(SLSj ,'yyyy') = to_char(sysdate,'yyyy')) 别名6,
(select count(参数) from 表3 where to_char(SLRQ ,'yyyy') = to_char(sysdate,'yyyy')) 别名7,
(select count(参数) from 表4 where to_char(SLSj ,'yyyy') = to_char(sysdate,'yyyy'))别名8
from dual
3.其中当时查询年数据的时候也稍微查了下,感觉这个方法挺好用的记录一下,可以方便年数据量、月数据量、日数据量的查询
select count(参数) from 表1 where to_char(对应sql的参数 ,'yyyy-MM') = to_char(sysdate,'yyyy-MM')) 别名
sysdate 函数直接用
4.欢迎大家交流