0

My question is very simple, but I can't seem to find the answer on here.

So,

All I want to do is select from two tables at once (with identical column names[id])

I currently have

"SELECT * FROM table1 WHERE id='$id_var'" 

but I also need to check 'table2' aswell. What's the best way to do this without creating a second query? thanks.

Shane

3 Answers 3

1
  SELECT * FROM table1 WHERE id='$id_var'
  UNION ALL
  SELECT * FROM table2 WHERE id='$id_var'
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT * FROM table1 WHERE id = '$id_var'
UNION ALL
SELECT * FROM table2 WHERE id = '$id_var'

However the question you should be asking from yourself is why do you have two tables with identical columns in the first place. Sounds like bad database design to me.

3 Comments

Thanks for your reply. Only the column NAMES are the same, the is data not. No bad design, this time.
yes agree, you may keep a boolean flag in database for separation !
@shane: That doesn't necessarily mean it's not bad design.
1

You could also do this:

SELECT table1.id, table1.x, table2.y 
FROM table1
LEFT JOIN table2 ON table1.id = table2.id
WHERE table1.id = $id;

1 Comment

The OP is very unclear, but it looks a LEFT JOIN or INNER JOIN is what he might want.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.