【问题标题】:SQL query to count number of Woocommerce orders by a list of usersSQL查询按用户列表计算Woocommerce订单数
【发布时间】:2020-05-08 14:01:00
【问题描述】:

使用 SQL 查询,是否可以统计每个用户 id 的 Woocommerce 订单数量?

SELECT customer_id FROM subscriptions
WHERE status = 'cancelled' GROUP BY customer_id

我已经生成了上面的用户 ID 列表,现在我需要找出每个用户有多少订单。如何使用 SQL 查询来做到这一点?

【问题讨论】:

    标签: mysql sql woocommerce


    【解决方案1】:

    您可以使用相关子查询:

    SELECT s.customer_id, count(*) AS Total,
           (SELECT COUNT(*)
            FROM subscriptions s
            WHERE wp.id = s.customer_id AND s.status = 'cancelled' 
           ) AS cancelledorders
    FROM wp_posts p
    GROUP BY s.customer_id;
    

    【讨论】:

    • 这开始朝着正确的方向发展,但您正在将客户 ID 与帖子 ID 匹配。它需要通过这些用户 id 找到所有已完成的订单
    • @Rob。 . .只需滑动表名。
    【解决方案2】:

    你试过count:

    column_to_count 是您要计算的列(例如订单可能...)

    SELECT customer_id, count(column_to_count)
    FROM subscriptions
    WHERE status = 'cancelled' GROUP BY customer_id
    

    然后你可以使用left join

    select sub.customer_id, count(1)
    from subscriptions sub
    left join wp_posts wp
    on wp.id = sub.customer_id
    where sub.status = 'cancelled';
    

    Here is a small demo so you can show us if somethin is different.

    【讨论】:

    • 这是我在另一个表中的 ID 列表,我需要通过 wp_posts 表运行它以查找每个用户 ID 的所有订单
    • 那么您需要向我们展示该 wp_posts 表的结构以及这两个表subscriptionswp_posts 之间的连接。连接是两个表中具有相同值的字段...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    相关资源
    最近更新 更多