我试图找到我存储的$ eastings $ northings坐标与我存储在mysql中的表中的Eastings / Northings坐标之间的距离。我正在使用Laravel。由于某种原因,查询无效。这是代码:
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)
答案 0 :(得分:0)
如果您根据计算字段(通过选择列表中的DB :: raw()生成)进行选择,而不是根据存储在数据库中的实际表列进行选择,那么您需要使用{{ 1}}子句而不是having()
子句。
这不是特别是Laravel / Eloquent或ORM问题,而是SQL的一般要求。