sql >> データベース >  >> RDS >> Mysql

MySQL LEFTOUTERJOINで最終結果の問題が発生しています

    これでうまくいくはずです:

    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
    

    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 |
    

    物事を少しスピードアップするために、いくつかのインデックス付けの恩恵を受けるかもしれません:

    create index idx_tbl_appointments_apptdate_timeid on tbl_appointments(AppointmentDate, AppointmentTimeID);
    
    create index idx_tbl_appointmentschedule_details_TimeID on tbl_appointmentschedule_details(AppointmentTimeID);
    

    クエリの変更:

    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
    

    SQLFiddleの例: http://sqlfiddle.com/#!9/0de6d7/1 >

    このクエリの2つの場所に日付スパンを追加したことに注意してください。このクエリがどのように実行されるかを確認してください。



    1. カートの更新-mysqlテーブルの更新-whileループ

    2. MariaDBでUNIONを使用する場合の「エラー1222(21000):使用されるSELECTステートメントの列数が異なる」を修正

    3. IPベースのクールダウン期間を提供するポータブルな方法は?

    4. 順序付けられていない方法で文字列を比較するOracle関数