0

I have that one-to-many relationship going here. Plus, not all "primary" records will have a joined record in the second database table.

Here are my tables:

tbl_customers tbl_addresses

A customer record in tbl_customers can obviously have many addresses, in the tbl_addresses table.

Here's my dilemma. There are many customer records with several addresses, and I assign one of those address records as the "main" address for a customer record. This is simply a single column called: primaryAddress. The value will be a number 1, if that address record is the main address.

So here is my query:

SELECT c.customername, a.state 
FROM `tbl_customers` c
LEFT JOIN `tbl_addresses` a ON c.customerid = a.customerid

That query will result in duplicate records for any customer with more than 1 address. If I add a little more to the LEFT JOIN:

SELECT c.customername, a.state 
FROM `tbl_customers` c
LEFT JOIN `tbl_addresses` a ON c.customerid = a.customerid AND a.primaryAddress = 1

This produces no results, even when I see in the database there are plenty of address records with primaryAddress = 1.

So I'm thinking my query is wrong.

Can someone see what I'm missing?

2
  • I should add that there are many customer records without an address record yet. I still need to display those customers, but leave the a.state as empty. (which is why I chose LEFT JOIN). Sep 22, 2011 at 18:29
  • 1
    I see nothing wrong with your second query as written. Is there anything else you've left out? Sep 22, 2011 at 18:41

1 Answer 1

0

To anyone who happens to land on this page looking for a solution:

THIS DOES NOT PROPERLY ANSWER THE QUESTION

The user was able to find the cause after discussing the problem here. Turns out he had a typo on his original code. The second block of SQL code posted on the question is correct, and should give the expected results. +1 for user JoeStefanelli for pointing this out.

Please read the comments for details. My original answer is below. Now I see I should have deleted it, instead of trying to amend it, as soon I realized it was flawed. At least the discussion helped the user find a solution...


My original answer:

You are almost there. Add a.primaryAddress = 1 to the WHERE clause instead.

EDIT

Not sure if this will exclude costumers with zero addresses from the results...

7
  • No. Adding that condition to the WHERE clause will force the JOIN to behave like an INNER JOIN. It is correct where it is. Sep 22, 2011 at 18:25
  • You're right. It excludes the customers with no addresses. darn it Sep 22, 2011 at 18:34
  • Yeah, that's what I though realized after answering. But I can't see why the second query on your question produces no results! I'm testing with a similar structure, and it works for me.
    – bfavaretto
    Sep 22, 2011 at 18:38
  • You're right! I mistyped the alias in my query box, if you can believe that. I love wasting time!! Thanks! Sep 22, 2011 at 18:49
  • -1 Even though this answer was accepted, it is incorrect and could be misleading for anyone who reads this question in the future. Sep 22, 2011 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.