A puzzle .. anyone?

Posts   
 
    
Pablo
User
Posts: 81
Joined: 21-Mar-2005
# Posted on: 14-Jan-2010 22:05:25   

Hello, Can anyone have a look a the following ...

I have 2 entities: Customers and Orders Customers and Orders are related via a CustomerId.

I want to select ALL customers, EXCEPT those customers that have previously ordered a product with ProductId=x

I've tried something like: select customers Where Order.ProductId != x

This seems to work, however, the customers who havent got any order yet, are not included, and I need those too!

Any help is appreciated! Paul

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 14-Jan-2010 23:59:41   

SELECT * FROM Customer WHERE CustomerId NOT IN (SELECT CustomerId FROM Order WHERE ProductId=x)

ie - get all of the customer ids that HAVE ordered product x, and then all of the customers that are not in this list - so this should get the ones that have not ordered anything yet as well.

All you have to do now is translate this to an LLBLGen or LINQ query - "left as an excercise for the reader" simple_smile

Matt

Pablo
User
Posts: 81
Joined: 21-Mar-2005
# Posted on: 15-Jan-2010 13:40:47   

Matt, thanx a million, I got stuck into thinking it was not possible, but hey, that cannot be possible....right? The solution is painfully simple. Many thanks ... Paul