gpt4 book ai didi

mysql - 从两个不相关的 MySQL 查询中添加总计

转载 作者:行者123 更新时间:2023-11-29 05:37:03 27 4
gpt4 key购买 nike

我正在努力找出正确的 MySQL 语法来添加两个查询的结果。查询从表中提取通话记录,都生成单个图形结果,我需要将它们加在一起。我确信这可以通过子查询来完成,但我似乎无法获得正确的语法。

查询如下:

SELECT SUM(costres) - SUM(costadmin)
FROM call_history, client
WHERE MONTH(start) = 3 AND YEAR(start) = 2012
AND call_history.client_reseller_id = client.id
AND client.charging_identifier <> 100000
AND client.charging_identifier <> 999999;

SELECT SUM(costcl) - SUM(costadmin)
FROM call_history, client
WHERE MONTH(start) = 3 AND YEAR(start) = 2012
AND call_history.client_reseller_id = client.id
AND client.charging_identifier = 100000
AND client.charging_identifier <> 999999;

正如我所说,我知道这很简单,但我似乎无法理解 MySQL 中的子查询语法!

提前致谢

乔治

最佳答案

怎么样-

SELECT
(SUM(IF(client.charging_identifier = 100000, costcl, 0)) - SUM(IF(client.charging_identifier = 100000, costadmin, 0))) +
(SUM(IF(client.charging_identifier <> 100000, costres, 0)) - SUM(IF(client.charging_identifier <> 100000, costadmin, 0)))
FROM call_history, client
WHERE MONTH(start) = 3 AND YEAR(start) = 2012
AND call_history.client_reseller_id = client.id
AND client.charging_identifier <> 999999;

关于mysql - 从两个不相关的 MySQL 查询中添加总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9822285/

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