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 6011069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:12:23+00:00 2026-05-23T02:12:23+00:00

I have a database with two tables that are similar to: table1 —— Id

  • 0

I have a database with two tables that are similar to:

table1
------
Id : long, auto increment
Title : string(50)
ParentId : long

table2
------
Id : long, auto increment
FirstName : string(20)
LastName : string(30)
Zip : string(5)

table2 has a one-to-many relationship with table1 where many includes zero.

I also have the following query (that works correctly, so ignore typos an the like, it is an example):

SELECT t1.Id AS tid, t1.Title, t2.Id AS oid, t2.FirstName, t2.LastName
    FROM table t1
    INNER JOIN table2 t2 ON t1.ParentId = t2.Id
    WHERE t2.Id IN
        (SELECT Id FROM table2
         WHERE Zip IN ('zip1', 'zip2', 'etc'))
    ORDER BY t2.Id DESC

The query finds all items in table1 that belong to a person in table2, where the person is in one of the listed zip codes.

The problem I have now is: I want to show all the users (with their items if available) in the listed zip codes, not just the ones with items.

So, I am wondering, should I just do something simple with a lot more queries, like:

SELECT Id AS oid, FirstName, LastName FROM table2 WHERE Zip in ('zip1', 'zip2', 'etc')
foreach(result) {
    SELECT Id AS tid, Title FROM table2 WHERE ParentId = oid
}

Or should I come up with a more elaborate single SQL statement? And if so, can I get a little help? Thanks!

  • 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-23T02:12:24+00:00Added an answer on May 23, 2026 at 2:12 am

    I agree with (and have upvoted) @Lee D and @Bueller. However, I generally advocate LEFT OUTER JOINS, because I find it easier to conceptualized what’s going on with them, particularly when you are joining three or more tables. Consider it like so:

    Start with what you know you want in the final result set:

    FROM table2 t2
    

    and then add in the “optional” data.

    FROM table2 t2
     left outer join table1 t1
      on t1.ParentId = t2.Id
    

    Whether or not matches are found, whatever gets selected from table2 will always appears.

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

Sidebar

Related Questions

I have a database with two tables ( Table1 and Table2 ). They both
I have a database two tables and a linking table that I need a
I have two tables in my database schema that represent an entity having a
I have two simple tables in my database. A card table that contains Id,
I have a database that has two tables, one of which contains a foreign
I have two very similar tables in our database, and I need to write
I have a database with two tables - let's call them Foo and Bar.
I have two database tables with the following structure: actions: action_id int(11) primary key
I have a database with two main tables notes and labels . They have
I have a simple database with two tables. Users and Configurations. A user has

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.