用php获取多个表数据

时间:2009-12-02 10:43:54

标签: mysql

假设我有两张桌子...... 我希望按日期发布/添加两个表数据顺序或按ID排序!

所以,如果我有

table1
id msg              date
2  this is msg      nowdate

table2
id comment          date
2  this is comment  nowdate

然后我如何通过id以单个查询顺序获取它?

2 个答案:

答案 0 :(得分:4)

UNION就是这个词,您正在寻找:

(SELECT * FROM table1) UNION (SELECT * FROM table2) ORDER BY id

答案 1 :(得分:2)

 select t1.*, (t1.id) as id1, (t2.id) as id2, (t1.date) as date1, (t2.date) as date2, t2.* from table1 t1 inner join table2 t2 on t1.id = t2.id order by t1.id

 $arr = mysql_fetch_asssoc(above_query);
 echo $arr['id1'] // id of first table
 echo $arr['id2'] // id of second table
 echo $arr['msg'] // msg of first table
 echo $arr['comment'] // comment of second table
 echo $arr['date1'] // date of first table
 echo $arr['date2'] // date of second table