加入 3 个表以查找购买了某个商品的新用户

Joining 3 tables to find new users who purchased a certain item

我在加入 2 个表时遇到了问题,这使我可以查看哪些新客户至少购买了 1 个特定商品。第一个从特定日期范围内提取新客户帐户,第二个提取在特定日期范围内至少购买过商品的客户

select customer.id from customer c
inner join device d on u.id = d.user_id
where c.created_date between {{start}} and {{end}}
group by c.id

select customer_id from purchase_history 
where item_id = {{itemID}} and created_date between {{starts}} and {{ends}} group by customer_id

我被连接部分卡住了,这总是让我感到困惑,但这是我现在所拥有的:

select customer.id from customer c 
INNER JOIN device d on u.id = d.user_id 
INNER JOIN
     (select customer_id from purchase_history 
where item_id = {{itemID}}
     ) c
     on c.customer = customer_id.purchase_history
where created_date between {{starts}} and {{ends}} 
group by customer_id

谢谢!!

主要目标是提供一个日期范围和一个商品 ID,该商品 ID 将拉取在特定日期范围内购买该商品(第二个代码)的新客户列表(第一个代码)。三张表(customer、device、purchase history)都有customer ID列。

这个怎么样:

select id
from customer c
  INNER JOIN device d on d.user_id = c.id
  INNER JOIN purchase_history ph on c.id = ph.customer_id
where ph.item_id = {{itemID}}
  and ph.created_date between {{starts}} and {{ends}} 
group by ph.customer_id

如果这没有帮助,也许您可​​以提供一个示例,说明您要加入的每个 table 以及您希望加入的内容。从您发布的内容来看,您似乎只需要客户 ID。