Mysql
 sql >> Baza danych >  >> RDS >> Mysql

MySQL LEFT OUTER JOIN ma problem z wynikami końcowymi

To powinno zrobić to za Ciebie:

select 
  m.AppointmentTimeID, 
  m.AppointmentDate, 
  coalesce(sum(ap.NumberOfApplicants),0) as NoOfApplicants

from (
  -- just basically doing a cross to get all time/date combinations
  select distinct d.AppointmentTimeID, a.AppointmentDate
  from tbl_appointments a, tbl_appointmentschedule_details d
) m

left join tbl_appointments ap 
  on ap.AppointmentTimeID = m.AppointmentTimeID
  and ap.AppointmentDate = m.AppointmentDate

where m.AppointmentDate between '2015-10-16' and '2015-10-17'

group by m.AppointmentDate, m.AppointmentTimeID
order by m.AppointmentDate, m.AppointmentTimeID

Przykład SQLFiddle:http://sqlfiddle.com/#!9/1e632/15

Results:
| AppointmentTimeID |           AppointmentDate | NoOfApplicants |
|-------------------|---------------------------|----------------|
|                22 | October, 16 2015 00:00:00 |              0 |
|                23 | October, 16 2015 00:00:00 |              3 |
|                24 | October, 16 2015 00:00:00 |              3 |
|                25 | October, 16 2015 00:00:00 |              1 |
|                26 | October, 16 2015 00:00:00 |              0 |
|                22 | October, 17 2015 00:00:00 |              4 |
|                23 | October, 17 2015 00:00:00 |              0 |
|                24 | October, 17 2015 00:00:00 |              0 |
|                25 | October, 17 2015 00:00:00 |              0 |
|                26 | October, 17 2015 00:00:00 |              0 |

Aby trochę przyspieszyć, możesz skorzystać z indeksowania:

create index idx_tbl_appointments_apptdate_timeid on tbl_appointments(AppointmentDate, AppointmentTimeID);

create index idx_tbl_appointmentschedule_details_TimeID on tbl_appointmentschedule_details(AppointmentTimeID);

Modyfikacja zapytania:

select 
  m.AppointmentTimeID, 
  m.AppointmentDate, 
  coalesce(sum(ap.NumberOfApplicants),0) as NoOfApplicants
from (
  select distinct AppointmentTimeID, AppointmentDate 
  from (select distinct AppointmentTimeID from tbl_appointmentschedule_details) one
  cross join
  (select distinct AppointmentDate from tbl_appointments
   where AppointmentDate between '2015-10-16' and '2015-10-17') two
) m
left join tbl_appointments ap 
  on ap.AppointmentTimeID = m.AppointmentTimeID
  and ap.AppointmentDate = m.AppointmentDate
where m.AppointmentDate between '2015-10-16' and '2015-10-17'
group by m.AppointmentDate, m.AppointmentTimeID
order by m.AppointmentDate, m.AppointmentTimeID

Przykład SQLFiddle:http://sqlfiddle.com/#!9/0de6d7/1

Zwróć uwagę, że w tym zapytaniu dodałem zakres dat w dwóch lokalizacjach. Zobacz, jak to zapytanie działa dla Ciebie.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. #1054 nieznana kolumna w przypadku błędu klauzuli

  2. Python Matplotlib oraz MySQL i Ginput

  3. Wykresy Primefaces 5.0 - jak dynamicznie tworzyć modele i serie wykresów całkowicie z wartości bazy danych

  4. Czy powinienem używać backticków, czy nie, podczas ucieczki słów kluczowych w MySQL?

  5. Błąd ładowania sterownika QMYSQL