如何将此SQL查询连接到一个查询中

时间:2013-12-17 13:45:10

标签: php mysql sql google-maps

我正在使用Google Maps API创建2点之间的路线。我的数据库中有一些要点,我只想找到距离道路“距离”的那些点。它工作,但我已经使用了路径mysqlQuery的foreach点,例如,对于一条路,我有327个查询(它会杀死我的数据库,所以我不得不去除它)

$query = sprintf("
    SELECT 
        id, address, name, 
        lat, lng, 
        ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance 
    FROM markers1 
    WHERE status > 0 
    HAVING distance < '%s' 
    ORDER BY distance LIMIT 0 , 20",

mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));

有谁知道如何减少它?

最诚挚的问候, Mateusz

编辑:

$radius = 12; //e.g
 /*
      $JSONdata have e.g array like:

      array(
            array( 15.003245,52.43246 ),
            array( 17.003245,45.48646 ),
            array( 11.003245,58.12246 )
           )

           Here are only 3 points, but on route I have usually more than 50

           */
 foreach( $JSONdata as $D ) {

      $center_lat = $D->nb;
      $center_lng = $D->ob;

         $query = sprintf("

    SELECT 
        id, address, name, 
        lat, lng, 
        ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance 
    FROM markers1 
    WHERE status > 0 
    HAVING distance < '%s' 
    ORDER BY distance LIMIT 0 , 20",

     mysql_real_escape_string($center_lat),
     mysql_real_escape_string($center_lng),
     mysql_real_escape_string($center_lat),
     mysql_real_escape_string($radius));

     $result = mysql_query($query);
     $Queries++;

     while ($row = @mysql_fetch_assoc($result)){

        $Results[ $row['id'] ] = $row;

     }
}

0 个答案:

没有答案