2009-12-02 116 views
1

多個表中的數據假設我有兩個表... 我想發佈日期/通過ID添加或爲了得到兩個表的數據順序!掌握PHP

因此,如果我有

table1 
id msg    date 
2 this is msg  nowdate 

table2 
id comment   date 
2 this is comment nowdate 

那我怎麼才能通過ID得到它在單個查詢訂單?

回答

4

UNION這個詞,你正在尋找:

(SELECT * FROM table1) UNION (SELECT * FROM table2) ORDER BY id 
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 
+0

但我怎麼可以顯示在單一mysql_fetch_array兩個表中的數據,而我有兩個不同的領域,即味精和評論 – testkhan 2009-12-02 10:56:37

+0

我已經更新了我回答,再看一遍plz – Sarfraz 2009-12-02 11:57:56