在select query cakephp中选择

时间:2016-03-23 04:07:38

标签: mysql cakephp

我有2张桌子


   1. feeds => id,name
   2. feed_locations => id, feed_id, latitude,longitude,location_name

这是我的cakephp代码



    $radious    =   '10';
    $this->paginate = array(
        'joins' => array(
            array('table' => 'feed_locations',
                'alias' => 'FeedLocation',
                'type' => 'LEFT',
                'conditions' => array(
                    'FeedLocation.feed_id = Feed.id',
                )
            )
        ),
        'limit'      => 10,             
        'fields' => array('id','name','(3959 * acos (cos ( radians('.$lat.') ) * cos( radians( FeedLocation.latitude ) ) * cos( radians( FeedLocation.longitude ) - radians('.$lng.') ) + sin ( radians('.$lat.') ) * sin( radians( FeedLocation.latitude )))) AS `distance`'),
        'recursive' => -1,
        'order'    =>  'distance ASC',
        'group' => 'Feed.id HAVING distance <'.$radious          
    );


它给我输出查询

SELECT 
    `Feed`.`id`, 
    `Feed`.`name`, 
    (3959 * acos (cos ( radians(40.7127837) ) * cos( radians( FeedLocation.latitude ) ) * cos( radians( FeedLocation.longitude ) - radians(-74.00594130000002) ) + sin ( radians(40.7127837) ) * sin( radians( FeedLocation.latitude )))) AS `distance`, 
    (Select COUNT(id) FROM feed_locations WHERE feed_id = `Feed`.`id`) AS `location_count` FROM `feeds` AS `Feed` 
    LEFT JOIN `feed_locations` AS `FeedLocation` ON (`FeedLocation`.`feed_id` = `Feed`.`id`) 
    GROUP BY `Feed`.`id` HAVING distance < 10 
    ORDER BY `distance` ASC

我的查询正在运行,但是发出了:

就像单个饲料有10个位置,如1m,2m,3m,4m,5m,10m,100m距离。我想找到5米距离的所有饲料然后它可以工作,但它告诉我这个饲料有5米。距离我的距离但结果应该是1米距离。
我想用cakephp语法

实现这个mysql查询
SELECT 
    a.id,
    a.name,
    a.distance,
    a.location_count
    FROM
      (SELECT
        `Feed`.`id`, 
        `Feed`.`name`, 
        (3959 * acos (cos ( radians(40.7127837) ) * 
        cos( radians( FeedLocation.latitude ) ) * 
        cos( radians( FeedLocation.longitude ) -
        radians(-74.00594130000002) ) + sin ( radians(40.7127837) ) * 
        sin( radians( FeedLocation.latitude )))) AS `distance`, 
          (Select COUNT(id) 
             FROM feed_locations 
             WHERE feed_id = `Feed`.`id`) AS `location_count` 
             FROM `feeds` AS `Feed` 
             LEFT JOIN `feed_locations` AS `FeedLocation` ON (`FeedLocation`.`feed_id` = `Feed`.`id`) 
        ORDER BY `distance` ASC) AS a
        GROUP BY a.id HAVING distance < 10;

此查询给出了确切的结果。请让我知道如何在cakephp中使用此查询

2 个答案:

答案 0 :(得分:1)

在CakePHP中,您还可以使用预准备语句,您可以使用SQL创建查询,查看文档:{​​{3}}

答案 1 :(得分:0)

我试过这段代码

function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra){
    $db         = $this->getDataSource();
    $modelName  =   $this->alias;   
    $result = $db->fetchAll(
        'SELECT 
        '.$modelName.'.id,
        '.$modelName.'.name,
        '.$modelName.'.distance,
        '.$modelName.'.location_count
        FROM
          (SELECT
            `Feed`.`id`, 
            `Feed`.`name`, 
            (3959 * acos (cos ( radians(40.7127837) ) * 
            cos( radians( FeedLocation.latitude ) ) * 
            cos( radians( FeedLocation.longitude ) -
            radians(-74.00594130000002) ) + sin ( radians(40.7127837) ) * 
            sin( radians( FeedLocation.latitude )))) AS `distance`, 
              (Select COUNT(id) 
                 FROM feed_locations 
                 WHERE feed_id = `Feed`.`id`) AS `location_count` 
                 FROM `feeds` AS `Feed` 
                 LEFT JOIN `feed_locations` AS `FeedLocation` ON (`FeedLocation`.`feed_id` = `Feed`.`id`) 
            ORDER BY `distance` ASC) AS '.$modelName.'
            GROUP BY '.$modelName.'.id HAVING distance < 10;'
    );  
    return $result;
}

它对我来说非常合适。
我在模型中管理这个查询。我在模型中创建一个函数paginate并提及此查询。然后它会自动对数据进行分页。