Jump to content

Sorting two different database queries alphabeticly


Fallen_angel

Recommended Posts

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


Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.