我有一个表action_info,其中每个用户点击保存。我需要获取前一周的非活动用户数。以下是ACTIVE用户的查询:
SELECT COUNT( DISTINCT user_id ) as active_for_week FROM action_info
WHERE action_name = 'attack'
AND time >= (CURDATE( ) - INTERVAL 7 DAY)
答案 0 :(得分:1)
这将排除上周从用户表中活动的所有用户,因此只为您提供非活动用户。我希望这就是你要找的东西。
SELECT COUNT(*) FROM users WHERE ID NOT IN(
SELECT DISTINCT user_id FROM action_info
WHERE action_name = 'attack'
AND time >= (CURDATE( ) - INTERVAL 7 DAY)
)
更新:
不查询用户表,您可以这样做:
SELECT COUNT(DISTINCT user_id) AS not_active_last_week FROM action_info
WHERE action_name = 'attack' AND user_id NOT IN (
SELECT DISTINCT user_id FROM action_info
WHERE action_name = 'attack'
AND time >= (CURDATE( ) - INTERVAL 7 DAY)
)
它给出了在某个时刻处于活动状态但在上周没有活动的所有用户的数量。 这是SQL fiddle