如何获取订单计数-SQL

时间:2018-08-18 19:59:04

标签: mysql count

每次尝试运行以下SQL时,我都会不断出现SQL错误(“字段列表”中的未知列“ s01_Orders.id”):

SELECT
  SUM(agg.Total) as orderTotal,
  SUM(agg.NoShip) as orderTotalNoShip,
  COUNT(s01_Orders.id) as ordercount
FROM
  (
    SELECT
      o.total AS "Total",
      o.total - oc.amount AS "NoShip"
    FROM
      s01_Orders o
      JOIN s01_OrderDiscountTotals odt ON o.id = odt.order_id
      LEFT JOIN s01_OrderCharges oc ON o.id = oc.order_id
      AND oc.type = "SHIPPING"
      AND o.ship_cntry = "US"
    WHERE
      odt.coupon_id = "1395"
    GROUP BY
      odt.order_id
  ) agg

当我省略以下内容时,它可以正常工作:

COUNT(s01_Orders.id) as ordercount

存在s01_orders表和列-任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

子查询没有s01_Orders.id的选择

  SELECT
  SUM(agg.Total) as orderTotal,
  SUM(agg.NoShip) as orderTotalNoShip,
  ordercount
  FROM
 (
SELECT
  o.total AS "Total",
  o.total - oc.amount AS "NoShip",
  COUNT(o.id) as ordercount
FROM
  s01_Orders o
  JOIN s01_OrderDiscountTotals odt ON o.id = odt.order_id
  LEFT JOIN s01_OrderCharges oc ON o.id = oc.order_id
  AND oc.type = "SHIPPING"
  AND o.ship_cntry = "US"
WHERE
  odt.coupon_id = "1395"
GROUP BY
  odt.order_id
) agg

尝试上述查询,可以选择计数,并且可以相应地工作。