如何将单个表中的多行分组并获得给定范围内的所有记录

时间:2019-04-21 19:29:13

标签: php mysql yii range between

我希望从旅馆的桌子上退回给定价格范围内的所有旅馆。 价格存储在四个不同的行中,例如价格一股,价格两股,价格三股,价格更多股,

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,
    ]);
} 

这是我已经写过的动作

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

这样,我想包括其余三行并得到我的结果。

这是我的表格行图像 http://www.clipular.com/posts/4991398571671552?k=Lud1W4LGJe5hSxOsl2ao7VOGguI

1 个答案:

答案 0 :(得分:0)

经过一番功课,我找到了答案

 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,
    ]);
}