Mysql左连接不按预期工作

时间:2015-04-05 18:04:59

标签: mysql left-join

我正在尝试一个简单的左连接,它给我带来了麻烦。我需要列出的所有客户(表a),无论他们是否在特定日期范围之间有发票(表b)。我的两次尝试都产生了唯一一个拥有该期间发票的客户:

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 个答案:

答案 0 :(得分:1)

在条件为内连接的情况下左连接,并根据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';