本文介绍了用于按用户列表计算 Woocommerce 订单数的 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 SQL 查询,是否可以计算每个用户 ID 的 Woocommerce 订单数量?
Using a SQL query, is it possible to count the number of Woocommerce orders for each user id?
SELECT customer_id FROM subscriptions
WHERE status = 'cancelled' GROUP BY customer_id
我已经生成了上面的用户 ID 列表,现在我需要找出每个用户有多少个订单.我如何使用 SQL 查询来做到这一点?
I've generated the list above of user id's, now I need to find out how many orders each one has. How can I do that using an SQL query?
推荐答案
您是否尝试过 count
:
column_to_count
是您要计算的列(例如订单可能...)
column_to_count
is the column you want to count(for example orders maybe...)
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';
这篇关于用于按用户列表计算 Woocommerce 订单数的 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!