简体   繁体   中英

No results for a query that's supposed to show sales in August for refunded customers in July

I'm using 2 tables - one of refunded ID's and one of Orders ID and I want to find the ID's that got any refund in July and spent money in August.

When I use this query I get no results:

Select ord.id
FROM `grouponi_groupon.tb_orders_refund` as ref, `grouponi_groupon.tb_orders_items` as ord
WHERE
ref.id = ord.id
and ref.last_update BETWEEN '2022-07-01' AND '2022-07-31'
and ord.last_update BETWEEN '2022-08-01' AND '2022-08-31'

Am I missing something?

I think a subquery is in order here. First step - Find the (unique) customers that got a refund in July:

SELECT DISTINCT userid
FROM tb_orders_refund
WHERE last_update BETWEEN '2022-07-01' AND '2022-07-31'

Step 2 - Show sales in August only for customers that we retrieved in the 1st step:

SELECT *
FROM tb_orders_items
WHERE last_update BETWEEN '2022-08-01' AND '2022-08-31'
AND userid IN (SELECT DISTINCT userid
               FROM tb_orders_refund
               WHERE last_update BETWEEN '2022-07-01' AND '2022-07-31')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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