简体   繁体   English

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

[英]how to group multiple rows from single table and get all record within given range

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 这是我的表行图像http://www.clipular.com/posts/4991398571671552?k=Lud1W4LGJe5hSxOsl2ao7VOGguI

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM