sql >> Database >  >> RDS >> Mysql

tel meerdere kolommen in één zoekopdracht

U kunt UNION ALL de afzonderlijke zoekopdrachten en pas voorwaardelijk toe aggregatie op de resulterende set:

select score,
       max(case when type = 'satisfaction' then count end) as satisfaction_count,
       max(case when type = 'response' then count end) as response_count,
       max(case when type = 'responder' then count end) as responder_count,
       max(case when type = 'service' then count end) as service_count             
from (
  select satisfaction_score as score, 
         count(satisfaction_score) as count, 
         'satisfaction' as type
  from j_survey_response
  where satisfaction_score != 0
  group by satisfaction_score

  union all

  select response_score,
         count(response_score) as count, 'response' as type
  from j_survey_response
  where response_score != 0
  group by response_score

  union all

  select responder_score,
         count(responder_score) as count, 'responder' as type
  from j_survey_response
  where responder_score != 0
  group by responder_score

  union all

  select service_score,
         count(service_score) as count, 'service' as type
  from j_survey_response
  where service_score != 0
  group by service_score) as t
group by score


  1. SQL-query om het aantal wederzijdse vrienden te vinden, gegeven een tabel met persoon, vriendenpaar

  2. Gegevens uit twee tabellen met dezelfde kolomnamen

  3. Stuur de huidige gps-coördinaten naar de mySQL-database met behulp van google maps api

  4. IN-clausule met NULL of IS NULL