开发者

mysql select query

My question is very s开发者_运维问答imple, 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


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


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜