Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Subtraia os resultados de duas consultas Select no MySQL


você pode simplesmente subtrair duas consultas ou mesclar
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 ;