2014-04-16 2 views
0

저는 저장 한 $ eastings $ northings 좌표와 mysql의 테이블에 저장 한 Eastings/Northings 좌표 사이의 거리를 찾으려고합니다. 나는 Laravel을 사용하고있다. 어떤 이유로 쿼리가 작동하지 않습니다.mysql 거리 쿼리가 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

'작동하지 않음'은 무엇을 의미합니까? 어떤 오류가 발생합니까? – Quasdunk

+1

거리를 계산하고 있으므로 –

+0

고맙습니다. –

답변

0

만약 당신이있어 계산 된 필드를 기준으로 선택 (DB 통해 생성 :: 당신의 선택 목록에() 원시)이 아니라, 여기에 코드입니다 데이터베이스에 저장된 실제 테이블 열보다 더 큰 경우 where() 절 대신 having() 절을 사용해야합니다.

이것은 특히 Laravel/Eloquent 또는 ORM 문제는 아니지만 SQL에 대한 일반적인 요구 사항입니다.

+0

감사합니다. 위의 자신의 의견을 보았습니다! –

관련 문제