1

Suppose i have two tables... I want to get two tables data order by date posted/added or order by id!

so that if i have

table1
id msg              date
2  this is msg      nowdate

table2
id comment          date
2  this is comment  nowdate

Then how can i get it in single query order by id?

2 Answers 2

4

UNION is the word, you are looking for:

(SELECT * FROM table1) UNION (SELECT * FROM table2) ORDER BY id
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

but how can i display data from both tables in single mysql_fetch_array while i have two different fields i.e msg and comment

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.