php mysql结果是为每个sid复制第一行

时间:2013-11-13 04:56:59

标签: php mysql

我正在尝试在php中创建一个基于地理坐标生成距离的mysql查询,但我的查询不断复制表格中第一个sid行的数据。如何获取我的查询以使用被调用的sid为每一行生成结果?

地理位置表:

fid | sid |纬度|经度

887 | 696 | 38.62774 | -90.199514

823 | 696 | 38.63979 | -90.02946

887 | 693 | 38.62774 | -90.199514

823 | 693 | 38.59430 | -90.260372

查询结果:

fid | sid |距离

887 | 696 | 0.05

887 | 696 | 0.05

887 | 693 | 0.05

887 | 693 | 0.05

渴望查询结果:

fid | sid |距离

887 | 696 | 0.05

823 | 696 | 3.10

887 | 693 | 0.05

823 | 693 | 12.02

PHP查询:

public function ListEntry( &$data ){

    $task = SPRequest::string('task', null );
    if (strtolower($task)!="search.results")
        return ;

    $session = JFactory::getSession();
    $ref_lat = $session->get('mj_rs_ref_lat', null);
    $ref_lng = $session->get('mj_rs_ref_lng', null);
    $ref_loc = $session->get('mj_rs_center_selector', null);
    $ref_dis = $session->get('mj_rs_ref_dist', null);
    if (!$ref_lat OR !$ref_lng)
        return ;

    $km     = $this->_getKm() ;
    $id     = $data['id'] ;
    $db     =& JFactory::getDBO();

    $query = " SELECT GEO.fid, GEO.sid, ";
    $query.= " ({$km}*acos(cos(radians({$ref_lat}))*cos(radians(GEO.latitude))*cos(radians(GEO.longitude)-radians({$ref_lng}))+sin(radians({$ref_lat}))*sin(radians(GEO.latitude)))) AS distance ";
    $query.= " FROM `#__field_geo` AS GEO  ";
    $query.= " WHERE  GEO.sid = {$id} ";
    $query.= " ORDER BY distance ASC ";
    $query.= " LIMIT 1 ";

    $db->setQuery($query);
    $distances = $db->loadAssoc();
    $fidgeo = $distances['fid'];
            $later = $distances['sid'];
    $distance = $distances['distance'];



    }

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

尝试在SQL查询中使用Distinct关键字

答案 1 :(得分:0)

尝试使用GROUP BY fid(并改为使用HERE) 它更好,因为mysql不再读取结果表。