Fallen_angel Posted November 24, 2006 Share Posted November 24, 2006 I was wondering if I can alphabeticly sort the search results of two different tables ? or can I just do each table alphabeticly one after the other ?if it is possible , how would I go about doing it ? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 24, 2006 Share Posted November 24, 2006 Can you post the table descriptions and tell us how you want to merge them? It depends very much on how you are combining the data. Quote Link to comment Share on other sites More sharing options...
Fallen_angel Posted November 24, 2006 Author Share Posted November 24, 2006 well I have my main database which has two different but very similar tables in it I would like to be able to list the contents of any search that i do ( where the field exists in both ofcourse ) and have them sorted alphabetcly for example the first query woudl be [code]$id = $_GET['id'];include "connect.php";$data=mysql_query(" SELECT * FROM reports where name_id='$id' ")[/code]and the seccond woudl be [code]$data2=mysql_query(" SELECT * FROM reports2 where name_id='$id' ")[/code]now I know that if I was jsut doign one or the other I would just add ORDER BY 'fieldname' and for each query I woudl put somethign like [code]while ($info= mysql_fetch_array($data) ) for the first and while ($info= mysql_fetch_array($data) )[/code]for the first and [code] while ($info2= mysql_fetch_array($data2) )[/code] for the seccond however I am not sure how to do it when combining the two or if i can at all thanx for any help you can give Quote Link to comment Share on other sites More sharing options...
btherl Posted November 24, 2006 Share Posted November 24, 2006 If the tables are identical, then you can do this[code=php:0]SELECT * FROM reports UNION reports2 where name_id = '$id'[/code]If they're not identical but you only want fields which are common to both, you can also do[code=php:0]SELECT * FROM (select id, field1, field2 FROM reports) UNION (select id, field1, field2 FROM reports2) where name_id = '$id'[/code]Then you can order that query.If you want to merge matching rows between the two tables and sort those merged rows, then that's totally different, that's a join. If you want that, post again :) Table definitions would help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.