2014-04-17 2 views
1

내 사이트를 다시 보내려면 Laravel 4를 사용하고 있습니다. 가장 어려운 부분은 쿼리를 Query Builder/Eloquent 형식으로 변환하는 것이 었습니다. 누군가가 내가 가장 길게 이것을 얻을 수 있도록 도울 수 있다면, 나는 매우 감사 할 것입니다!Laravel, join with subquery에서이를 수행하는 방법

SELECT zipcode, city, state, lat, lng, distance_in_mi 
FROM (
SELECT zipcode, city, state, lat, lng, r, (3963.17 * ACOS(COS(RADIANS(latpoint)) * COS( RADIANS(lat)) * COS(RADIANS(longpoint) - RADIANS(lng)) + SIN(RADIANS(latpoint)) * SIN(RADIANS(lat)))) AS distance_in_mi 
FROM zipcode 
JOIN (
SELECT $current_lat AS latpoint, $current_lng AS longpoint, 10 AS r 
) AS p 
WHERE lat 
BETWEEN latpoint - (r /69) 
AND latpoint + (r /69) 
AND lng 
BETWEEN longpoint - (r/(69 * COS(RADIANS(latpoint)))) 
AND longpoint + (r/(69 * COS(RADIANS(latpoint)))) 
) d 
WHERE distance_in_mi <= r 
ORDER BY distance_in_mi 

최근 시도 :

$data_object = DB::table('zipcode', function($query) 
      { 
     $query->select('zipcode, city, state, lat, lng, r, (3963.17 * ACOS(COS(RADIANS(latpoint )) * COS(RADIANS(lat)) * COS(RADIANS(longpoint) - RADIANS(lng)) + SIN(RADIANS(latpoint))  * SIN(RADIANS(lat)))) AS distance_in_mi') 
      ->from('zipcode') 
      ->join('zipcode', function($query1) 
      { 
      $query1->select("($current_lat AS latpoint, $current_lng AS longpoint, 10 AS r) AS p") 
        ->whereBetween('lat', 'latpoint - (r /69)') 
        ->whereBetween('lng', 'longpoint - (r/(69 * COS(RADIANS(latpoint))))  AND longpoint + (r/(69 * COS(RADIANS(latpoint))))'); 

      }) 
      }) 
        ->where('distance_in_mi', '<=', 'r') 
        ->orderBy('distance_in_mi') 
        ->get(); 

답변

1

당신은 조인 및 하위 선택이 필요하지 않습니다. 그것이 당신을 위해 일해야 할 것입니다 :

DB::table('zipcode') 
->select(['zipcode','city','state','lat','lang', 
    DB::raw("(3963.17*ACOS(COS(RADIANS(latpoint))*COS(RADIANS(lat))*COS(RADIANS(longpoint)-RADIANS(lng))+SIN(RADIANS(latpoint))*SIN(RADIANS(lat)))) 
     AS distance_in_mi, $current_lat AS latpoint, $current_lng AS longpoint, 10 AS r")]) 
->orderBy('distance_in_mi') 
->havingRaw('lat BETWEEN latpoint - (r/69) 
    AND latpoint + (r/69) 
    AND 
    lng BETWEEN longpoint - (r/(69*COS(RADIANS(latpoint)))) 
    AND longpoint + (r/(69*COS(RADIANS(latpoint)))) 
    AND distance_in_mi <= r') 
->get(); 
+0

우선, 정말 고마워요. 나는 그것 (실종 된 것)과 a를 시도했다. 게재 위치에 대한 제안 사항이 있으십니까? –

+0

) 실종 쿼리를 계속 가져 오지 못했습니다. –

+0

물론, 나쁘다. 이제 편집되었습니다.)] select() 끝 부분에 누락되었습니다. 지금 시도하십시오 –