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。太简单了。我的许多查询都没有用到用例。非常感谢。