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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:46:30+00:00 2026-05-19T23:46:30+00:00

table1 (ids always exist) +—-+——+ | id | col1 | +—-+——+ | 1 |

  • 0

table1 (ids always exist)

+----+------+
| id | col1 |
+----+------+
| 1  |  ab  |
+----+------+
| 2  |  gh  |
+----+------+

table2 (ids always exist)

+----+------+
| id | col2 |
+----+------+
| 1  |  cd  |
+----+------+
| 2  |  ij  |
+----+------+

table3 (ids might be missing, in this case 2 is missing)

+----+------+
| id | col3 |
+----+------+
| 1  |  ef  |
+----+------+

PHP

$col = 'ab';

$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2, table3 AS t3
WHERE t1.id = t2.id AND t2.id = t3.id AND (t1.col1 = '$col' OR t2.col2 = '$col'
OR t3.col3 = '$col) GROUP BY t1.id, t2.id, t3.id");

That would only work if all three tables had “the same id” included, but what happens if an “id” is missing in table3 for whatever reason? how can I still test for all three tables and get t1.id to output 1, when $col = ab? would I have to use left join?

$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2 
LEFT JOIN (SELECT id FROM table3 WHERE col3 = '$col') AS t3 ON t3.id = t1.id 
WHERE t1.id = t2.id AND (t1.col1 = '$col' OR t2.col2 = '$col')
GROUP BY t1.id, t2.id");

what am I doing wrong here?

  • 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-19T23:46:30+00:00Added an answer on May 19, 2026 at 11:46 pm

    What are you doing wrong? Querying a table that doesn’t exist. That’s always going to raise an error.

    I’m not going to address the wisdom of designing a database in which tables crucial to your queries come and go.

    Your only hope on the client side is to

    • test for the existence of the tables
      you’re interested in, and
    • execute different SQL statements
      based on those results.

    [After your edit]

    It sounds like you need either one or two left outer joins. This gives you all the ids that are common to both table1 and table2, regardless of whether they’re in table3.

    select t1.id, t2.id, t3.id
    from table1 t1
    inner join table2 t2 on (t1.id = t2.id)
    left join table3 t3 on (t1.id = t3.id);
    

    And this gives you all the ids that are in table1, regardless of whether they’re in table2 or table 3.

    select t1.id, t2.id, t3.id
    from table1 t1
    left join table2 t2 on (t1.id = t2.id)
    left join table3 t3 on (t1.id = t3.id);
    

    And, of course, you can filter the results with your WHERE clause.

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

Sidebar

Related Questions

Table1: ID,Name,some more columns Table2: ID Table3: Name I want get output from table1,
I have a database with two tables ( Table1 and Table2 ). They both
I have 2 tables. Table1 has fields A, B, C, D and Table2 has
I have a table that represents a series of matches between ids from another
i have a table called category in which i have main category ids and
I have 2 tables. One (domains) has domain ids, and domain names (dom_id, dom_url).
I've deleted some records from a table in a SQL Server database. The IDs
Table1: Everything including the kitchen sink. Dates in the wrong format (year last so
How would you reference table1 columns to 2 columns in table 2 I created
I am having a table Table1 with columns id1, id2, id3 all the columns

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.