开发者

Customer and Order Sql Statement

开发者 https://www.devze.com 2022-12-11 06:51 出处:网络
TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primar开发者_开发技巧y key of Cust

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primar开发者_开发技巧y key of CustomerID. The table Order contains a primary key of OrderID and a foreign key of CustomerID.


Something like

Select yourcustomerfields, yourorderfields
From Customer
Left join Orders on Customer.OrderID = Orders.OrderID


I came up with this solutions.

Select CustomerName
from Customer
Where pk_CustomerID IN
(
Select fk_CustomerID from Orders
INNER JOIN Customer
on Customer.pk_CustomerID=Orders.fk_CustomerID)

/* NOT IN instead of IN will give the other customers who doesn't have an Order */

0

精彩评论

暂无评论...
验证码 换一张
取 消