SQL如何获得0作为输出(HAVING COUNT())

时间:2017-07-28 03:42:52

标签: mysql having

我的sql查询有问题。我想在查看之前获得没有(0)订单的订单的客户ID列表,并且在查看之前获得一(1)个订单的订单。 以下查询为我提供了第三行(count_compl_ord_before_d_1)的预期结果,但第二行没有结果(count_compl_ord_before_d_0):

SELECT created_at AS d, customer_id AS b,
(SELECT customer_id FROM orders WHERE customer_id = b AND created_at < d AND status = 'complete' HAVING COUNT(created_at) = 0 ) AS count_compl_ord_before_d_0,
(SELECT customer_id FROM orders WHERE customer_id = b AND created_at < d AND status = 'complete' HAVING COUNT(created_at) = 1 ) AS count_compl_ord_before_d_1
FROM orders
WHERE status = 'complete'
AND created_at >= '2017-06-08'

但是,当我运行以下内容时,我会得到所有不同的金额,从0到xxx为count_compl_ord_before_d:

SELECT created_at AS d, customer_id AS b,
  (SELECT COUNT(created_at) FROM orders
   WHERE customer_id = b AND created_at < d
     AND status = 'complete') AS count_compl_ord_before_d
FROM orders
WHERE status = 'complete'
AND created_at >= '2017-06-08'

HAVING COUNT(created_at) = 0是否有问题?

我不太了解SQL,但我想了解更多,所以我很感激解释。

提前致谢!干杯

0 个答案:

没有答案