Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6612105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:03:20+00:00 2026-05-25T20:03:20+00:00

I have that one-to-many relationship going here. Plus, not all primary records will have

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:03:21+00:00Added an answer on May 25, 2026 at 8:03 pm

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Type MenuItem that has one-to-many relationship with itself through Children property.
I have 2 NHibernate entities (tables) that are in a one to many relationship.
I have a many-to-one relationship that users can edit via checkboxes. PK of Foo
one Article has many Comments, and i want to fetch all Articles that have
I have a Vessel object that has a one-to-many relationship with a VesselDetail object.
I have two tables that have a one-to-many relationship table1 (one), table2 (many). table1
I have a query that always returns one row, with many columns. I would
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many
I have a workbook that has many checkboxes (form control) on one tab. I
This issue may have be raised here before, but I am not confident that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.