dongxu1668 2019-04-21 19:29
浏览 154

如何从单个表中对多个行进行分组并获取给定范围内的所有记录

I want all the hostel within the given price range from my hostel's table. price is stored into four different rows like price one share, price two shares, price three shares, price more share,

public function actionNearbyHostel($min,$max,$types,$lat,$long){   
    $latitude = $lat;
    $longitude = $long;
    $type = $types;
    $max_price = (int)$max;
    $min_price = (int)$min;
    $hostels = Hostels::find ()->select("*,(6371 * 
        acos(cos(radians({$latitude}))* 
        cos(radians(`lat`)) * cos(radians(`log`) - radians({$longitude})) + 
        sin(radians({$latitude})) * sin(radians(`lat`)))) AS distance")
            ->having("distance<:distance")
            ->addParams([ 
                ':distance' => 5 
            ])->where([
                'status' => Hostels::ACTIVE,
                'type' => $type
            ])->andWhere(['between','price_one_share',$min_price,$max_price])
    ->all();
    return $this->render('filterhostel',[
        'hostels' => $hostels,
    ]);
} 

This is my action where I already have written

andWhere(['between','price_one_share',$min_price,$max_price])

like this, I want to include the remaining three rows and get my result.

this is my table row image http://www.clipular.com/posts/4991398571671552?k=Lud1W4LGJe5hSxOsl2ao7VOGguI

  • 写回答

1条回答 默认 最新

  • duanpai9945 2019-04-23 13:57
    关注

    After some homework, I found the answer

     public function actionNearbyHostel($min,$max,$types,$lat,$long){   
        $latitude = $lat;
        $longitude = $long;
        $type = $types;
        $max_price = (int)$max;
        $min_price = (int)$min;
        $hostels = Hostels::find ()->select("*,(6371 * acos(cos(radians({$latitude}))* 
        cos(radians(`lat`)) * cos(radians(`log`) - radians({$longitude})) + 
        sin(radians({$latitude})) * sin(radians(`lat`)))) AS distance")
        ->having("distance<:distance")
        ->addParams([ 
        ':distance' => 5 
        ])->where([
        'status' => Hostels::ACTIVE,
        'type' => $type
        ])->andWhere(['or',
        ['between','price_one_share',$min_price,$max_price],
        ['between','price_two_share',$min_price,$max_price],
        ['between','price_three_share',$min_price,$max_price],
        ['between','price_more_share',$min_price,$max_price]
        ])
        ->all();
        return $this->render('filterhostel',[
            'hostels' => $hostels,
        ]);
    }  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100