2015-04-05 131 views
0

我正在嘗試一個簡單的左連接,給我的問題。我需要列出的所有客戶(表a),無論他們是否在某個日期範圍內有發票(表b)。我的兩個嘗試,已經取得了具有這一時期的發票只有客戶:Mysql左連接不能正常工作

select b.clientname,a.* from invdata a 
    left join clidata b on a.clidataid=b.recordid 
     where b.recstatus=1 and b.isactive=1 
     and a.reccreate between '2015-04-01 00:00:00' and '2015-04-30 23:59:59'; 

OR

select a.clientname,b.* from clidata a 
    left join invdata b on a.recordid=b.clidataid 
     where a.recstatus=1 and a.isactive=1 
     and b.reccreate between '2015-04-01 00:00:00' and '2015-04-30 23:59:59'; 

一點幫助,請。謝謝!!

回答

1

LEFT JOIN與WHERE條件內加入,將過濾數據按WHERE條件,您可能需要將where條件進入加盟條件

select b.clientname,a.* from invdata a 
    left join clidata b on a.clidataid=b.recordid 
    and b.recstatus=1 and b.isactive=1 
    and a.reccreate between '2015-04-01 00:00:00' and '2015-04-30 23:59:59'; 
+0

哇。什麼菜鳥的錯誤。謝謝阿比克! – user1532602 2015-04-05 18:13:09