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 > Data Access, Manipulation & Batch Languages > ANSI SQL > left join problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-25-05, 07:02
shylock shylock is offline
Registered User
 
Join Date: Jun 2003
Posts: 7
Question left join problem

Hello,
Two tables :
customer -> customerid
address -> customerid,addresstype


a customer can have more types of addresses :
postaddress,privateaddress

I want to show all customers with their addresses.
They addresses must be of type 'postaddress'
If a customer doesn't have a address of type 'postaddress' then still show the record with the customerfields and empty address fields.

query : select * from customers,address where
customer.customerid = address.customerid (+) and
addresstype = 'postaddress'

This query shows only the customers with "postaddresses" but not the customers without a address or customers with a "privateaddress".

So I am looking for a query that shows the customers with postaddresses and also the customers with empty or other type of addresses but then they addressfields should be empty.
Who can help me ?
Thanx Christian
Reply With Quote
  #2 (permalink)  
Old 01-25-05, 07:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
i don't understand that weird (+) syntax, so i hope you understand standard sql
Code:
select c.customerid
     , c.customername 
     , a.address
  from customers as c
left outer
  join address as a
    on c.customerid = a.customerid 
   and a.addresstype = 'postaddress'
in the above query, notice that the addresstype condition is in the ON clause

this means it is a condition of the join

let's imagine a customer without a postaddress

since this is a condition of the join, that customer will be returned with NULL in the address

by comparison, consider what happens when the addresstype condition is in the WHERE clause --
Code:
select c.customerid
     , c.customername 
     , a.address
  from customers as c
left outer
  join address as a
    on c.customerid = a.customerid 
 WHERE a.addresstype = 'postaddress'
now suppose there is a customer with no address at all

in a LEFT OUTER join, this customer is also returned with NULL in the address

but when the WHERE clause is evaluated, NULL is not equal to 'postaddress' so that customer is filtered out!

this in effect reverts the results of the join to the same as if you had specified an INNER join, and you get only customers with a postaddress
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-25-05, 07:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
please don't cross-post

see http://www.dbforums.com/t1097989.html
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

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