mysql离开了join和sum group by

时间:2010-09-27 02:20:18

标签: sql mysql join

您好我使用此mysql代码生成以下数据:

代码:

SELECT L.LEAVETYPE, LA.IDLEAVETYPE, LA.IDLEAVETABLE, SUM( LA.NOOFDAYS ) 
FROM LEAVETYPE AS L
LEFT JOIN LEAVEAPPLICATION AS LA ON LA.IDLEAVETYPE = L.IDLEAVETYPETABLE
GROUP BY L.LEAVETYPE

alt text

当我添加Where条件时,它只会显示带有值的LeaveType。如何显示所有的leavetype,包括null(如第一张图片中所示)?

SELECT L.LEAVETYPE, LA.IDLEAVETYPE, LA.IDLEAVETABLE, SUM(LA.NOOFDAYS)
FROM LEAVETYPE AS L
LEFT JOIN LEAVEAPPLICATION AS LA ON LA.IDLEAVETYPE = L.IDLEAVETYPETABLE
WHERE LA.IDSTAFFTABLE='24'
GROUP BY L.LEAVETYPE

alt text

1 个答案:

答案 0 :(得分:1)

将idstafftable条件移动到连接而不是where:

LEFT JOIN LEAVEAPPLICATION AS LA ON LA.IDLEAVETYPE = L.IDLEAVETYPETABLE AND LA.IDSTAFFTABLE='24'