Select 个上次下订单早于 X 日期的帐户
Select accounts where last order placed was older than X date
我正在尝试确定最后一次在特定日期或之前下订单的购物者。
我想这样做是为了识别过时的帐户并取消订阅邮件。
到目前为止我有这个查询,但它似乎正在检索比指定日期最近订购的帐户 - 这不是我想要的!
SELECT
s.id,s.first_name,s.last_name,s.email, latest_orders.last_order_date, au.verified, au.unh
FROM
(SELECT
shopper_id, MAX(order_date) AS last_order_date
FROM
order_list WHERE order_date <= ?
GROUP BY
shopper_id) AS latest_orders
INNER JOIN
shoppers s
ON
s.id = latest_orders.shopper_id
JOIN auth_users au ON au.shopper_id=s.id
WHERE s.mail_outs='Y' LIMIT 50
使用having
:
SELECT shopper_id, MAX(order_date) AS last_order_date
FROM order_list
GROUP BY shopper_id
HAVING MAX(order_date) <= ?;
如果您想了解有关购物者的所有信息,请返回 shoppers
:
SELECT s.*, os.last_order_date
FROM shoppers s JOIN
(SELECT shopper_id, MAX(order_date) AS last_order_date
FROM order_list
GROUP BY shopper_id
HAVING MAX(order_date) <= ?
) os
ON os.shopper_id = s.id;
我正在尝试确定最后一次在特定日期或之前下订单的购物者。 我想这样做是为了识别过时的帐户并取消订阅邮件。
到目前为止我有这个查询,但它似乎正在检索比指定日期最近订购的帐户 - 这不是我想要的!
SELECT
s.id,s.first_name,s.last_name,s.email, latest_orders.last_order_date, au.verified, au.unh
FROM
(SELECT
shopper_id, MAX(order_date) AS last_order_date
FROM
order_list WHERE order_date <= ?
GROUP BY
shopper_id) AS latest_orders
INNER JOIN
shoppers s
ON
s.id = latest_orders.shopper_id
JOIN auth_users au ON au.shopper_id=s.id
WHERE s.mail_outs='Y' LIMIT 50
使用having
:
SELECT shopper_id, MAX(order_date) AS last_order_date
FROM order_list
GROUP BY shopper_id
HAVING MAX(order_date) <= ?;
如果您想了解有关购物者的所有信息,请返回 shoppers
:
SELECT s.*, os.last_order_date
FROM shoppers s JOIN
(SELECT shopper_id, MAX(order_date) AS last_order_date
FROM order_list
GROUP BY shopper_id
HAVING MAX(order_date) <= ?
) os
ON os.shopper_id = s.id;