我的第一个 SQL 查询是:
SELECT username,COUNT(username) as total_active_users FROM users WHERE active = '1' GROUP BY referrer // $act_user
统计每个推荐人的活跃用户
第二个sql查询是:
SELECT COUNT(orders) as total_user_orders FROM users_orders GROUP BY $act_user['username'] // the username from the first query.
计算我从第一个查询中获得的每个用户的订单总数
我试图做LEFT JOIN
的是只计算至少有 1 个订单的活跃用户
问题是:我有 2 个不同GROUP BY
的第一个是“推荐人”,第二个是“用户名”
我正在尝试做类似的事情:
SELECT u.username, COUNT(u.username) as total_active_users, COUNT(b.orders) as
total_user_orders FROM users u LEFT JOIN users_orders b on u.username = b.username
WHERE u.active = '1' AND total_user_orders >= '1' GROUP BY (u.referrer for u) and (b.username for b)
请问有什么想法吗?