首页 新闻 搜索 专区 学院

这样的sql能优化吗,每次子查询只有条件不同

0
悬赏园豆:10 [已解决问题] 解决于 2015-05-16 16:59
1 select * from
2 (select count(*) as '计划任务' from hscdm_project_qc where task_date BETWEEN date_add(now(),interval -7 DAY ) and NOW() ) as t1,
3 (select count(*) as '报送数据' from hscdm_project_qc where task_status =2 and task_date BETWEEN date_add(now(),interval -7 DAY ) and NOW()  )as t2,
4 (select count(*) as '审核通过' from hscdm_project_qc where check_status=1 and task_date BETWEEN date_add(now(),interval -7 DAY ) and NOW()  ) as t3

服务器端和客户端都好 

阿哈太阳的主页 阿哈太阳 | 初学一级 | 园豆:1
提问于:2015-05-14 15:44
< >
分享
最佳答案
0

count(case when .. then ...  end)

 

SELECT
    COUNT(1) AS '计划任务',
    COUNT(CASE WHEN TASK_STATUS = 2 THEN 1 END) AS '报送数据',
    COUNT(CASE WHEN TASK_STATUS = 1 THEN 1 END) AS '审核通过'
FROM
    HSCDM_PROJECT_QC
WHERE
    TASK_DATE BETWEEN DATE_ADD(NOW(), INTERVAL - 7 DAY) AND NOW() 
收获园豆:10
过于执著 | 菜鸟二级 |园豆:339 | 2015-05-15 09:08
其他回答(2)
0

哪里需要优化呢?

【秦时明月】 | 园豆:803 (小虾三级) | 2015-05-14 15:55

大部分内容都是一样的 就只有个别条件不同  想看看能不能优化一下

支持(0) 反对(0) 阿哈太阳 | 园豆:1 (初学一级) | 2015-05-14 15:57
0

可以试下 case 。。。。 then 。。。when语句

wangYiYi | 园豆:13 (初学一级) | 2015-05-14 16:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册