使用groupby返回零计数值mysql

时间:2016-09-25 10:16:51

标签: mysql sql group-by

附加的查询不会为每个年份的日期返回零值,如何返回零值,预期结果:

  • 201802 1 1
  • 201801 2 0
  • 201753 0 1

而不是

  • 201802 1 1
  • 201753 0 1

1 个答案:

答案 0 :(得分:0)


您好Mostafa,您可以使用以下查询,

SELECT Yearweek(`date`) AS yweek_m, 
   Week( `date` ) AS weekperiod_m, 
   Count(id_xx) AS cnt_m 
FROM measurementview MV
    LEFT OUTER JOIN woundview  WV
      ON MV.wound_id_wound = WV.id_wound 
    LEFT OUTER JOIN activepatientview AV
      ON WV.patient_id_patient = AV.id_patient 
    WHERE 
    MV.Yearweek( `date` ) >= MV.Yearweek(NOW()) - 3 
    AND WV.user_id_user = 4
    AND AV.clinic_id_clinic = 3
GROUP BY yweek_m, weekperiod_m
ORDER BY yweek_m DESC