如何加入,包括具有NULL数据的行 [英] How to JOIN, including row with NULL data
本文介绍了如何加入,包括具有NULL数据的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题就是这样返回客户及其订单,包括未下订单的客户.
我已经写了这样的代码
SELECT A.custid, A.companyname,
B.orderid, B.orderdate
FROM Sales.Customers as A
JOIN sales.orders as B
ON A.custid=B.custid;
它几乎可以正常工作,只有一点,它不返回这样的行,其中orderid
和orderdate
等于NULL
It works almost right, except one point, it doest return such rows, where orderid
and orderdate
equal NULL
custid companyname orderid orderdate
------ ------------- ------ ---------
57 Customer WVAXS NULL NULL
此外,我不明白为什么我的代码无法正常工作.
In addition, I cant understand why my code doesn`t work properly.
推荐答案
为此使用左连接:
SELECT A.custid, A.companyname,
B.orderid, B.orderdate
FROM Sales.Customers as A
LEFT JOIN sales.orders as B
ON A.custid=B.custid;
左联接包括客户"表中的所有行,并且仅包含订单表中发生匹配的行,因此B的列可以为空.
A left join includes all rows from the Customers table and only the ones in the orders table where a match occurs -- thus you can have null for B's columns.
这篇关于如何加入,包括具有NULL数据的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文