我寫了兩個mysql查詢,其中一個提取我在一年的特定月份中出現的總用戶(註冊),另一個提取當年特定月份中的活動用戶。我需要找到當年不活躍用戶的數量。爲此,我正在考慮減去通過兩個單獨查詢獲得的totalUsers和activeUsers列。 下面是查詢在Mysql中減去兩個Select查詢的結果
1. Fetch Total Registered users
set @numberOfUsers := 0;
SELECT T.createdMonth, T.monthlyusers, (@numberOfUsers := @numberOfUsers + T.monthlyusers) as totalUsers
FROM
(
SELECT month(from_unixtime(u.createdDate)) as createdMonth, count(u.id) as monthlyusers
FROM user u
where year(from_unixtime(u.createdDate)) = '2016'
group by month(from_unixtime(u.createdDate))
) T;
2. Fetch Active Users
select month(from_unixtime(lastActive)), (count(u.id)) as activeUsers from user u
where year(from_unixtime(lastActive)) = '2016'
group by month(from_unixtime(lastActive));
我需要從totalUsers減去activeUsers。我如何實現這一目標?
發現一個StackOverflow的答案,回答是[這裏](http://stackoverflow.com/questions/6715095/how-to-sum-and-subtract - 使用-SQL)。 – IAMZERG