gpt4 book ai didi

mysql - SQL:包含 where 子句的计数语句的总和

转载 作者:太空宇宙 更新时间:2023-11-03 11:43:18 26 4
gpt4 key购买 nike

我有 3 个计数语句,我需要将它们合并并求和。

SELECT COUNT(status) 
FROM tableA
WHERE env='test'
AND deploy_date='2016-10-19'
AND platform='MA'
AND server='B'
AND status='5';

SELECT COUNT(status)
FROM tableA
WHERE env='test'
AND deploy_date='2016-10-19'
AND platform='MA'
AND server='B'
AND status='3';

SELECT COUNT(status)
FROM tableA
WHERE env='test'
AND deploy_date='2016-10-19'
AND platform='MA'
AND server='B'
AND status='1';

我试过了

SELECT (select count(status) 
FROM tableA
WHERE env='test'
AND deploy_date='2016-10-19'
AND platform='MA'
AND server='B' and status='5'; ) +
(SELECT COUNT(status)
FROM tableA WHERE env='test'
AND deploy_date='2016-10-19'
AND platform='MA'
AND server='B' AND status='3';) +
(SELECT COUNT(status)
FROM tableA
WHERE env='test'
AND deploy_date='2016-10-19'
AND platform='MA'
AND server='B' AND status='1';)

但是出现语法错误。我错过了什么?有没有更好的方法在 MySQL 中完成此操作。提前致谢

最佳答案

这里有一种组合方式

SELECT count(1)
FROM tableA
WHERE env = 'test'
AND deploy_date = '2016-10-19'
AND platform = 'MA'
AND server = 'B'
AND status IN ( '5', '3', '1' );

关于mysql - SQL:包含 where 子句的计数语句的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40277308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com