简体   繁体   English

Mysql左联接不能按预期工作

[英]Mysql Left Join Not working As Expected

I am attempting a simple left join that is giving me problems. 我正在尝试一个简单的左联接,这给我带来了问题。 I need all the customers listed (table a) regardless of whether or not they have an invoice (table b) between a certain date range. 我需要列出的所有客户(表a),无论他们在特定日期范围内是否有发票(表b)。 Both of my attempts have yielded the only customer that has an invoice for that period: 我的两次尝试都产生了唯一拥有该期间发票的客户:

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';

Little help, please. 请帮忙。 Thanks!! 谢谢!!

Left join with where condition is inner join and will filter the data as per the where condition, you may need to move the where condition into joining condition 左联接,其中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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM