0

我有一个表 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)
4

1 回答 1

0

这将从用户表中排除上周活跃的所有用户,因此只为您提供非活跃用户。我希望这就是你要找的。

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 小提琴

于 2012-12-21T12:11:48.940 回答