If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Other > LEFT OUTER JOIN with WHERE

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-10, 07:16
tosa tosa is offline
Registered User
 
Join Date: Feb 2010
Posts: 35
LEFT OUTER JOIN with WHERE

I'm stuck again!

How can I do a successful LEFT OUTER JOIN with a WHERE constraint on the right table?

Consider this SQL statement:
"SELECT Qty, ItemCode, Description, IFNULL(Attributes.QtyValue,1) FROM PUB.QEvent
LEFT OUTER JOIN PUB.Attributes ON QEvent.Key=Attributes.Key
WHERE Attributes.Name= 'Item'"

It's simplified, but I think I get my point through. All items does not have an attribute. If I leave out WHERE I get rows I don't want from table Attributes, but when using WHERE I do not get the rows that don't have an attribute because of the WHERE constraint.
Reply With Quote
  #2 (permalink)  
Old 04-14-10, 08:08
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by tosa View Post
I'm stuck again!

How can I do a successful LEFT OUTER JOIN with a WHERE constraint on the right table?
The WHERE clause essentially turns the OUTER JOIN into an INNER JOIN.

You need to move the "where" clause into the JOIN condition:
Something like this:
Code:
SELECT Qty, 
       ItemCode, 
       Description, 
       IFNULL(Attributes.QtyValue,1) 
FROM PUB.QEvent 
   LEFT OUTER JOIN PUB.Attributes 
        ON QEvent.Key=Attributes.Key AND Attributes.Name= 'Item'
Reply With Quote
  #3 (permalink)  
Old 04-14-10, 08:15
tosa tosa is offline
Registered User
 
Join Date: Feb 2010
Posts: 35
Yes, that did it.

Thanks a lot!
Reply With Quote
  #4 (permalink)  
Old 08-17-10, 17:41
eurekaplanet eurekaplanet is offline
Registered User
 
Join Date: Aug 2010
Posts: 1
Hi,
A left outer join the number of rows returned but not in the same order. If the number of columns in the select changes the order of results. It should not happens when you use a simple join. I have to choose ordering it in my sql. This order in which it returns is essential for the the application to run properly.
Is this the same result?
Reply With Quote
  #5 (permalink)  
Old 08-17-10, 19:32
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,540
Quote:
Originally Posted by eurekaplanet View Post
This order in which it returns is essential for the the application to run properly.
you must use an ORDER BY clause in the SELECT statement

without the ORDER BY clause, your results are unpredictable
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
join, outer, progress

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On