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 primary key of CustomerID. The table Order contains a primary key of OrderID and a foreign key of CustomerID.
+3
A:
Something like
Select yourcustomerfields, yourorderfields
From Customer
Left join Orders on Customer.OrderID = Orders.OrderID
Andrew
2009-11-16 01:44:43
A:
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 */
jero
2009-11-16 04:15:46
@jero: I am sorry but those `pk_` and `fk_` prefixes gotta go.
Sung Meister
2009-11-17 04:59:38