sql >> Database teknologi >  >> RDS >> Mysql

Træk resultater fra to Select-forespørgsler fra i MySQL

du kan blot trække to forespørgsler fra eller flette

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 ;



  1. codeigniter mysql venstre join include select

  2. Oracle-sekvens, der starter med 2 i stedet for 1

  3. Indsæt data i tabel med resultat fra en anden udvalgt forespørgsel

  4. Genbruge en parameter i en PreparedStatement?