【问题标题】:Joining 3 tables to find new users who purchased a certain item加入 3 个表以查找购买了某个项目的新用户
【发布时间】:2023-04-02 03:16:02
【问题描述】:

我在加入 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 将提取在特定日期范围内购买该特定商品(第二个代码)的新客户(第一个代码)列表。这三个表(客户、设备和购买历史)都包含客户 ID 列。

【问题讨论】:

    标签: mysql join inner-join outer-join metabase


    【解决方案1】:

    这个怎么样:

    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
    

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

    【讨论】:

    • 是的!因此,我想在给出日期范围和项目 ID 后提取客户 ID(在客户表上),以查看哪些新客户(在设备表上)进行了购买(在购买历史上)。现在,它一直在给我一个匹配 ids 的错误。
    • 了解 created_date 列在 purchase_history 表中,我编辑了上述查询。如果这不起作用,你能粘贴你得到的完整错误吗?
    • 如果这对您有用,您介意选择我的答案作为正确答案吗?谢谢!
    • 观察一下,sql没有问题,上面的解决方法是有效的
    猜你喜欢
    • 2021-12-04
    • 1970-01-01
    • 2022-10-17
    • 2019-01-29
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2020-07-30
    相关资源
    最近更新 更多