我正在尝试查找我存储的$eastings$northings坐标与我存储在mysql表中的eastings/northings坐标之间的距离。我在用拉拉威尔。由于某些原因,查询不起作用。这是代码:
public function postAnalysis(){
$relevant_home = Input::get('relevant_homes');
$asset = Home::where('Home_name', '=' , $relevant_home)->first();
$Eastings = $asset->Eastings;
$Northings = $asset->Northings;
/*
$competitors = Home::raw('SELECT SQRT(POW($Eastings - `Eastings`,2) + POW($Northings - `Northings`,2)) as distance FROM homes HAVING distance <=10 ORDER BY distance ASC')->get();
echo $competitors;
*/
$competitors = DB::table('homes')
-> select(DB::raw('SQRT(POW('.$Eastings.' - Eastings,2) + POW('.$Northings.' - Northings,2)) AS distance'))
-> where('distance', '<', 10)
-> get();
echo $competitors;
}
显示的错误为:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'where clause' (SQL: select SQRT(POW(390120 - Eastings,2) + POW(298935 - Northings,2)) AS distance from `homes` where `distance` < 10)
如果根据计算字段(通过选择列表中的DB::raw()生成)进行选择,而不是根据存储在数据库中的实际表列进行选择,则需要使用having()
子句而不是where()
子句。
这不是Laravel/Eloquent或ORM问题,而是SQL的一般要求。