在MYSQL中对子查询执行计数总和

时间:2017-11-03 14:04:32

标签: mysql sql

我正在尝试对从订单的子查询计数收到的所有订单执行求和,但我收到错误说错误代码:1242。子查询返回超过1行 < / p>

查询:

select sum((select count(orderNo) as count from  order r group by r.status 
having r.status in ('Open','shipped In Progress','New')));

2 个答案:

答案 0 :(得分:1)

所以我找到了一种对我有用的方法。

select sum(a.count) 
from(select count(o.order) as count from order o )a

答案 1 :(得分:-1)

只需删除GROUP BY并使用WHERE

即可
select sum(  (select count(orderNo) as count 
              from  order r 
              WHERE r.status in ('Open','shipped In Progress','New')
             )
          );

实际上您不需要SUM或子查询:

SELECT count(orderNo) as count 
FROM   order r 
WHERE r.status in ('Open','shipped In Progress','New')