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

Hvordan er det bedre at samle nogle få tabeller (inklusive underforespørgsler) i én forespørgsel?

Jeg tror, ​​at dette giver dig det, du leder efter (en af ​​mange måder):

SELECT t.diapason, COUNT(*) AS 'number_of_users'
FROM (SELECT CASE  
    when amount<100 then '0-100' 
    when amount>=100 then '100 +' END AS diapason
    FROM 
        (SELECT SUM(amount) AS amount 
        FROM payments 
        INNER JOIN (SELECT DISTINCT user_id FROM activity where login_time between '2012-04-05' and '2012-04-12') AS a ON payments.user_id = a.user_id
        GROUP BY payments.user_id) AS P
      ) t
GROUP BY t.diapason
  ORDER BY number_of_users desc
;



  1. Præcis forskel mellem Oracle Client og Oracle Database

  2. Løsning til Insert Intention Locks i MySQL

  3. PostgreSQL DISTINCT ON med forskellig ORDER BY

  4. Hvordan erstatter man NULL med 0 i forespørgslen?