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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:03:10+00:00 2026-06-11T00:03:10+00:00

I am trying to make a query where 3 tables are being used. The

  • 0

I am trying to make a query where 3 tables are being used. The first, table1, is used to store id values for table2 and table3 as well as some other data. In some cases there are no values stored in either of table2 or table3, some cases involve one or the other, and some involve both.

The issue is that when there are not values in either of table2 or table3, SQL attempts to look up NULL values as follows:

SELECT table1.id, table2value, table3value
FROM table1, table2, table3
WHERE table1value1 = table2.id AND table1value2 = table3.id;

So in this query if table1value1 or table1value2 are NULL, the query will not work as it is obviously not a valid link. So I am wondering how I would go about finding only the values that exist.

  • 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-06-11T00:03:12+00:00Added an answer on June 11, 2026 at 12:03 am

    Use INNER JOIN instead. It joins the tables together with the condition of existing data in all tables. Else that row won’t be returned.

    SELECT table1.id, table2.value, table3.value
    FROM table1
    INNER JOIN table2
    ON table1.value1 = table2.id
    INNER JOIN table3
    ON table1.value2 = table3.id;
    

    But if you require for only one of these 2 rows to have existing data, you can do like this:

    SELECT table1.id, table2.value, table3.value
    FROM table1
    LEFT JOIN table2
    ON table1.value1 = table2.id
    LEFT JOIN table3
    ON table1.value2 = table3.id;
    WHERE table2.id IS NOT NULL OR table3.id IS NOT NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a query in two tables: SIMPLE_PERSON with 3 fields (name,
I am trying to make one query, to get some statistic data from database.
I'm trying to make a SELECT on three relational tables like these ones: table_materials
We're currently stuck trying to make a simple (?) SQL query. We have these
I inherited a new system and I am trying to make some improvements on
I am trying to make a multi-table query that I am not quite sure
I am trying to make a query in Codeigniter that will allow a user
I have a mysql database with two tables: table1 (id, name, emailid) table2 (id,
I am trying to make a query with Hibernate criteria API. It looks simple
I'm trying to make an address book. And have made my tables like this:

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.