1

我有这个代码:

select count(distinct affiliate_orders_id) as count
, sum(affiliate_value) as total
, sum(affiliate_payment) as payment 
from " . TABLE_AFFILIATE_SALES . " a 
    left join " . TABLE_ORDERS . " o on (a.affiliate_orders_id = o.orders_id)
where a.affiliate_orders_id = o.orders_id 
and o.orders_status >= " . AFFILIATE_PAYMENT_ORDER_MIN_STATUS . "
        ";

  $affiliate_sales_query= tep_db_query($affiliate_sales_raw);
  $affiliate_sales= tep_db_fetch_array($affiliate_sales_query);

因此,$affiliate_sales['total'] = 128000 实际上应该是 32000,因为有多个affiliate_values 和affiliate_orders_id。affilaite_values 一些具有相同的值,因此它们不能不同。affilaite_orders_id 具有所有唯一值,但有多个行并且需要不同。然后,affiliate_values 必须根据affiliate_orders_id 的不同行求和以获得准确的总和。

我正在尝试获取所有affiliate_values 的总和,以确定表中有多少不同的affiliate_orders_id。

4

1 回答 1

0

根据您的更新,我认为这将为您提供所需的内容。您需要使用子查询

SELECT COUNT(a) COUNT, SUM(av) total, SUM(ap) aptotal
FROM (
    SELECT affiliate_orders_id a, affiliate_value av, SUM(affiliate_payment) AS ap
from " . TABLE_AFFILIATE_SALES . " a 
    left join " . TABLE_ORDERS . " o on (a.affiliate_orders_id = o.orders_id
    GROUP BY affiliate_orders_id, affiliate_value)
where a.affiliate_orders_id = o.orders_id 
and o.orders_status >= " . AFFILIATE_PAYMENT_ORDER_MIN_STATUS . ") a

现在这导致了一个更大的问题。您是否缺少表中的连接条件?通常,您不应该在查询中返回重复的日期,因此我建议您首先仔细检查您的查询是否正确加入。

于 2013-03-20T14:33:53.900 回答