如何从单个表中对多行进行分组并获取给定范围内的所有记录
Posted
技术标签:
【中文标题】如何从单个表中对多行进行分组并获取给定范围内的所有记录【英文标题】:how to group multiple rows from single table and get all record within given range 【发布时间】:2019-04-21 19:29:12 【问题描述】:我想要在我的旅馆桌子上给定价格范围内的所有旅馆。 价格存储在四个不同的行中,例如价格一股,价格二股,价格三股,价格更多股,
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】:做了一些功课,我找到了答案
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,
]);
【讨论】:
以上是关于如何从单个表中对多行进行分组并获取给定范围内的所有记录的主要内容,如果未能解决你的问题,请参考以下文章
SQL - 显示给定范围内的所有日期,并使用数据库中的时间戳计算该日期有多少帖子