Mysql
 sql >> Base de données >  >> RDS >> Mysql

Soustraire les résultats de deux requêtes Select dans MySQL

vous pouvez simplement soustraire deux requêtes ou fusionner

select (select query) - (select query);

SET @numberOfUsers := 0; 

SELECT l.totalUsers-a.monthlyusers FROM (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 ) l,(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))) a ;