我想知道正确的左连接

时间:2012-07-16 10:35:49

标签: mysql

我有这个查询,我希望必须根据给定的条件显示表mstemp的所有记录。

SELECT 
    mstEmp.empname, InDate, dailyattendance.InTime,
    dailyattendance.OutTime, mstEmp.teamtype 
FROM 
    mstEmp
LEFT JOIN 
    dailyattendance ON (dailyattendance.HolderName = mstEmp.empname ) 
WHERE 
    mstEmp.teamtype = '$chk' 
    AND (InDate BETWEEN '$from' AND '$to' )
ORDER BY 
    InDate ASC

但似乎这个查询无效。

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT mstEmp.empname, InDate, dailyattendance.InTime,
       dailyattendance.OutTime, mstEmp.teamtype 
FROM mstEmp
  LEFT JOIN dailyattendance 
    ON (dailyattendance.HolderName = mstEmp.empname ) AND (InDate BETWEEN '$from' AND '$to' )
WHERE mstEmp.teamtype = '$chk' 
ORDER BY InDate ASC