Brendan Posted February 28, 2006 Share Posted February 28, 2006 I have a search engine written in php and mysql. There are two tables, one with a username and many colums with various data, but not dates, and another table with the same usernames but this one has dates. Now when you search you can change it to search and sort with certain fields in the first table, but i dont know how to make it sort with the second field, because generally you sort with the first table you query not the second.I have it set up generally like this right now: $sql=mysql_query("select*from firsttable $where limit $start, $end"); if(!empty($sql)){ while($row=mysql_fetch_array($sql)){ $user=$row["username"]; $blah=$row["blah"]; $sqlq=mysql_query("select*from secondtable where username='$user' ORDER BY creationdate $order"); if(!empty($sqlq)){ while($row=mysql_fetch_array($sqlq)){ $creationdate=$row["creationdate"]; Link to comment https://forums.phpfreaks.com/topic/3726-search/ Share on other sites More sharing options...
camdagr81 Posted February 15, 2007 Share Posted February 15, 2007 Try: ORDER BY ID Link to comment https://forums.phpfreaks.com/topic/3726-search/#findComment-185499 Share on other sites More sharing options...
craygo Posted February 15, 2007 Share Posted February 15, 2007 Why not combine the query's and then you can sort it anyway you like[code]<?php$order = $_POST['orderby']; // get this value from the form submitted$sql = "SELECT * FROM firsttable AS t1 LEFT JOIN secondtable AS t2 ON t1.username = t2.username ORDER BY $order ASC";?>[/code]Now it doesn't matter which table the order is in, as long as it is unique to the table. If you want to sort by username, you will get an error if both tables have a field called username. You will have to sort that out before the queryRay Link to comment https://forums.phpfreaks.com/topic/3726-search/#findComment-185517 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.