tags:

views:

38

answers:

4

I have a fairly conventional set of order entry tables divided by:

Orders OrdersRows OrdersRowsOptions

The record in OrderRowOptions is not created unless needed. When I create a set of joins like

select * from orders o 
  inner join OrdersRows r on r.idOrder = o.idOrder 
  inner join ordersrowsoptions ro on ro.idOrderRow = r.idOrderRow
where  r.idProduct = [foo]

My full resultset is blank if no ordersrowsoptions records exist for the given product.

what's the correct syntax to return records even if no records exist at one of the join clauses?

thx

+1  A: 

LEFT JOIN / RIGHT JOIN.

Edit: yes, the following answer, given earlier, is correct:

select * from orders o  
  inner join OrdersRows r on r.idOrder = o.idOrder  
  left join ordersrowsoptions ro on ro.idOrderRow = r.idOrderRow 
where  r.idProduct = [foo] 
Tobiasopdenbrouw
And why the -1, dear anonymous vandal? The question was: "what's the correct syntax to return records even if no records exist at one of the join clauses?" Other answers here are similar, but not downvoted.
Tobiasopdenbrouw
Really needs more info than this. Elaborate on your answer. I didn't downvote, but i did consider it. :)
cHao
I was typing more answer, and during that typing saw someone else had given a more complete one. I didn't want to 'scoop' the more complete answer (which would be identical to mine) by doing a quick edit. But now I will edit it, as you like.
Tobiasopdenbrouw
Down vote removed now more information given. Sorry, I'll down vote myself for not clicking add comment earlier.
Paul Hadfield
+3  A: 
select * from orders o  
  inner join OrdersRows r on r.idOrder = o.idOrder  
  left join ordersrowsoptions ro on ro.idOrderRow = r.idOrderRow 
where  r.idProduct = [foo] 

Of course you should not use select * in any query but especially never when doing a join. The repeated fields are just wasting server and network resources.

Since you seem unfamiliar with left joins, you probably also need to understand the concepts in this: http://wiki.lessthandot.com/index.php/WHERE_conditions_on_a_LEFT_JOIN

HLGEM
A: 

LEFT JOIN (or RIGHT JOIN) are probably what you are looking for, depending on which side of the join no rows may appear.

Brian Hooper
A: 

Interesting, do you want to get all orders that have that product in them? The other post is correct that you have to use LEFT or RIGHT OUTER JOINS. But if you want to get entire orders that have that product then you'd need a more complex where clause.

Paul Hadfield