Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用1个查询获取2个查询的结果_Mysql - Fatal编程技术网

Mysql 使用1个查询获取2个查询的结果

Mysql 使用1个查询获取2个查询的结果,mysql,Mysql,我在同一个表中有以下两个查询事务 交易表中有以下两列与此相关,registrationId、total和totalPaid 要获取所有已支付交易,我运行以下查询 Select SUM(transactions.totalPaid) as netPaid from transactions where deleted is null group by registrationId Select SUM(transactions.total - transactions.totalPaid) as

我在同一个表中有以下两个查询
事务

交易
表中有以下两列与此相关,
registrationId
total
totalPaid

要获取所有已支付交易,我运行以下查询

Select SUM(transactions.totalPaid) as netPaid from transactions where deleted is null group by registrationId
Select SUM(transactions.total - transactions.totalPaid) as unPaid from transactions where ((totalPaid < total) OR (total < 0)) and deleted is null group by registrationId
要获取所有未支付的交易,我运行以下查询

Select SUM(transactions.totalPaid) as netPaid from transactions where deleted is null group by registrationId
Select SUM(transactions.total - transactions.totalPaid) as unPaid from transactions where ((totalPaid < total) OR (total < 0)) and deleted is null group by registrationId
从((totalPaid
如何组合查询并一次性获得两个结果


谢谢

您可以在
未付
列中使用
案例
语句

Select sum(transactions.totalPaid) as netPaid
    , sum(case when (totalPaid < total) OR (total < 0) then transactions.total - transactions.totalPaid else 0 end) as unPaid
from transactions 
where deleted is null 
group by registrationId
选择sum(transactions.totalPaid)作为netPaid
,金额(当(totalPaid
@Metal-duh。太简单了。我的许多查询都没有用到用例。非常感谢。