0

我正在尝试计算在线商店客户的积分使用情况。随着时间的推移,客户获得积分。当积分被兑换时,customer.points的值被修改以反映剩余要兑换的积分。获得的任何额外积分也会添加到customer.points中。因此,确定客户在帐户生命周期内获得的积分数量的唯一真正机制是将总使用量与剩余积分相加(order.total + customer.points)。

下面的查询返回所需的结果,但仅适用于已兑换积分的客户。我想要的是,因为所有客户都有积分,所以也能够为那些没有兑换积分的人返还积分余额。

SELECT customer.store_id, customer.customer_id, `order`.total + customer.points 
    AS allpoints, customer.firstname, customer.lastname 
    FROM `order`
    INNER JOIN customer ON `order`.customer_id = customer.customer_id
    WHERE (customer.store_id =3)
    GROUP BY customer.customer_id
4

1 回答 1

0

听起来您需要使用 an left outer join,它将返回联接左侧表的所有行,并且仅在存在记录时才返回右侧表的行。这确实意味着当记录不存在时您需要处理表的null值。order

SELECT
  customer.store_id,
  customer.customer_id,
  isnull(order.total, 0) + customer.points AS allpoints,
  customer.firstname,
  customer.lastname
FROM customer
  LEFT OUTER JOIN order ON order.customer_id = customer.customer_id
WHERE (customer.store_id =3)
GROUP BY customer.customer_id
于 2012-10-30T21:14:22.253 回答