异常查询 - 来自两个表的匹配错误

时间:2015-01-26 14:00:37

标签: mysql

我有两个表,我想运行异常。

表1 'Retail'

零售业中的专栏如下。

Branch_Name, Customer_Name, Document_Number

表2 'Doc'

Branch_Name, Customer_Name, Invoice_Number

现在文件编号和发票编号是一回事。

我希望查询向我显示的是,我们在Doc中没有多少记录。

实施例。 '零售'

Branch_Name, Customer_Name, Document_Number
sat             BOSS           s11121
bat             Kiven          s25456

表2 'Doc'

Branch_Name, Customer_Name, Invoice_Number
sat             BOSS           s11121

结果

我们失踪了,

Branch_Name, Customer_Name, Invoice_Number
bat            Kiven         s25456

谢谢

3 个答案:

答案 0 :(得分:1)

以下内容将生成Retail中的所有行,并且只生成Doc中没有相应/相同行的记录。

select *
from Retail r
  left outer join Doc d on r.Branch_Name = d.Branch_Name
    and r.Customer_Name = d.Customer_Name
    and r.Document_Number = d.Invoice_Number
where d.Branch_Name is null;

答案 1 :(得分:0)

试试这个:

 select retail.* from doc where Retail left join doc on Document_number =Invoice_Number where Document_number is null

答案 2 :(得分:0)

尝试使用左连接加入两个表,然后选择Doc表中为null的列,如:

SELECT *
FROM Retail r LEFT JOIN DOC d
ON   r.Branch_Name = d.Branch_Name
AND  r.Document_Number = d.Invoice_Number
WHERE d.Invoice_Number IS NULL