How to order by results from 2 seperate tables in PHP and MySQL.

Posted by Vafello on Stack Overflow See other posts from Stack Overflow or by Vafello
Published on 2010-03-19T15:37:46Z Indexed on 2010/03/19 18:01 UTC
Read the original article Hit count: 188

Filed under:
|
|
|
|

I am trying to output results of 2 sql queries to one JSON file. The problem is that I would like to order them ascending by distance which is the result of equation that takes homelat and homelon from the users table and lat, lng from locations table.(basically it takes lattitude and longitude of one point and another and computes the distance between these points). Is it possible to take some parameters from both select queries, compute it and output the result in ascending order?

$wynik = mysql_query("SELECT homelat, homelon FROM users WHERE guid='2'") or 
die(mysql_error()); ;

$query = "SELECT * FROM locations WHERE timestamp";
$result = map_query($query);
$points = array();

while ($aaa = mysql_fetch_assoc($wynik)) {

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    array_push($points, array('name'=>$row['name'], 'lat'=>$row['lat'], 
'lng'=>$row['lng'], 'description'=>$row['description'], 
'eventType'=>$row['eventType'], 'date'=>$row['date'],
        'isotime'=>date('c', ($row['timestamp'])), 'homelat'=>$aaa['homelat'], 
'homelon'=>$aaa['homelon']));

}
echo json_encode(array("Locations"=>$points));

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql