tags:

views:

226

answers:

1

Is it possible in SubSonic to make a query like this

SELECT * FROM [customer] 
LEFT OUTER JOIN [orders] ON [customer].[customerid] = [orders].[customerid] 
AND [orders].[deptid] = 2

I tried this in with the following SubSonic (2.1) code :

SqlQuery q = DB.Select().From<Customer>()
    .LeftOuterJoin(orders.customerid, customer.customerid)
    .AndExpression(orders.deptid).IsEqualTo(2);

When i look in SQL profiler what query has been generated, it is :

SELECT * FROM [customer] 
LEFT OUTER JOIN [orders] ON [customer].[customerid] = [orders].[customerid] 
WHERE [orders].[deptid] = 2

The key is the difference in the AND [orders].[deptid] = 2 and the WHERE [orders].[deptid] = 2, which off course will result in 2 different resultsets.

Is this something i can do with SubSonic at all or is the SubSonic query i made not correct?

Thanks for any help!!

Edit : For example i have 2 customers, 1 has several orders and the second customer has none. An example of the data with only the join (SELECT customerid, orderid, deptid FROM [customer] LEFT OUTER JOIN [orders] ON [customer].[customerid] = [orders].[customerid]) will return this :

row A : customerid 1, orderid 1, deptid 2

row B : customerid 1, orderid 2, deptid 1

row C : customerid 2, orderid NULL, deptid NULL

The SQL with the AND statement will return row A and C. I believe this is because the AND is evaluated during the join process.

The SQL with the WHERE statement will only return row A because the WHERE is evaluated after the join and then only row A has deptid 2.

A: 

The key is the difference in the AND [orders].[deptid] = 2 and the WHERE [orders].[deptid] = 2, which off course will result in 2 different resultsets.

Both will return the same resultset. There may be some performance difference, but i dont thinks, so. as modern database, optimize the query better.

Smart Pandian
I edited my question with some sample data. I just checked this in SQL Server 2005 and there is a difference between the 2 resultsets. With the WHERE statement not all customers are returned and with the AND statement they always are. And in my case i do need both customers.
Marco