我有一个很长的MYSQL查询,它从四个表中选择一个总数,但其中一个子查询返回多行并导致错误。
这是我的疑问:
select created,
(select coalesce(sum(equal_to_dollar), 0) from capitals group by created ) as capital,
(select coalesce(sum(incoming), 0) - coalesce(sum(outgoing), 0) from transactions where currency = 'دالر') +
(select coalesce(sum(equal_to_dollar), 0) from transactions where incoming != 0 ) -
(select coalesce(sum(equal_to_dollar), 0) from transactions where outgoing != 0 ) as total_transaction,
(select coalesce(sum(incoming), 0) - coalesce(sum(outgoing), 0) from temporary_clients where currency = 'دالر') +
(select coalesce(sum(equal_to_dollar), 0) from temporary_clients where incoming != 0 ) -
(select coalesce(sum(equal_to_dollar), 0) from temporary_clients where outgoing != 0 ) as total_temp_client,
(select coalesce(sum(outgoing), 0) - coalesce(sum(incoming), 0) from money_transmission where currency = 'دالر') +
(select coalesce(sum(equal_to_dollar), 0) from money_transmission where outgoing != 0 ) -
(select coalesce(sum(equal_to_dollar), 0) from money_transmission where incoming != 0 ) as total_transmission,
(select coalesce(total_transaction + total_temp_client + total_transmission, 0)) as total,
(select capital - abs(coalesce(total_transaction + total_temp_client + total_transmission, 0))) as result
from capitals group by created
在此查询中,第一个选择created
会返回多个值,而以as capital
结尾的第一个子查询也会返回多个值。
此查询应返回资本计算结果,其中包含数据库中每个日期的其他三个表的总和
我检查了许多具有相同错误的问题,所有问题都在IN
条款中使用了inner join
或where
,但我认为我的情况有所不同,我不知道该怎么做。
如果您有任何想法我可以帮助我如何解决这个问题
谢谢你:))
答案 0 :(得分:1)
尝试类似:
select created,c.sum_dollars
(select coalesce(sum(incoming), 0) - coalesce(sum(outgoing), 0) from transactions where currency = 'دالر') +
(select coalesce(sum(equal_to_dollar), 0) from transactions where incoming != 0 ) -
(select coalesce(sum(equal_to_dollar), 0) from transactions where outgoing != 0 ) as total_transaction,
(select coalesce(sum(incoming), 0) - coalesce(sum(outgoing), 0) from temporary_clients where currency = 'دالر') +
(select coalesce(sum(equal_to_dollar), 0) from temporary_clients where incoming != 0 ) -
(select coalesce(sum(equal_to_dollar), 0) from temporary_clients where outgoing != 0 ) as total_temp_client,
(select coalesce(sum(outgoing), 0) - coalesce(sum(incoming), 0) from money_transmission where currency = 'دالر') +
(select coalesce(sum(equal_to_dollar), 0) from money_transmission where outgoing != 0 ) -
(select coalesce(sum(equal_to_dollar), 0) from money_transmission where incoming != 0 ) as total_transmission,
(select coalesce(total_transaction + total_temp_client + total_transmission, 0)) as total,
(select capital - abs(coalesce(total_transaction + total_temp_client + total_transmission, 0))) as result
from capitals
JOIN (select coalesce(sum(equal_to_dollar), 0) as sum_dollars, created from capitals group by created ) c ON capitals.created = c.created
group by created
我无法真正测试因为我不知道你的数据库架构。无论如何你应该加入那些结果集。