views:

24

answers:

3

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

+1  A: 
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.

Matti Virkkunen
Thanks for your reply. Only the column NAMES are the same, the is data not. No bad design, this time.
shane
yes agree, you may keep a boolean flag in database for separation !
Tumbleweed
@shane: That doesn't necessarily mean it's not bad design.
Matti Virkkunen
+1  A: 
  SELECT * FROM table1 WHERE id='$id_var'
  UNION ALL
  SELECT * FROM table2 WHERE id='$id_var'
Michael Pakhantsov
+1  A: 

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;
the_void
The OP is very unclear, but it looks a LEFT JOIN or INNER JOIN is what he might want.
chryss