简体   繁体   English

如何获取订单数量 - SQL

[英]How to get order count - SQL

I keep getting and SQL error (Unknown column 's01_Orders.id' in 'field list') every-time I try to run the following SQL:每次尝试运行以下 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

It works fine when I omit the following:当我省略以下内容时它工作正常:

COUNT(s01_Orders.id) as ordercount

The s01_orders table and column exist - any help would be greatly appreciated. s01_orders 表和列存在 - 任何帮助将不胜感激。

The Subquery doesn't have the selection for s01_Orders.id子查询没有 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

Try the above query, this has the selection of count and will work accordingly.试试上面的查询,这有计数的选择,并会相应地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM