2016-08-19 3 views
1

보고서를 만들려면 조인 쿼리를 작성해야합니다. 내가 지금 SQL에서 조인 쿼리를 쓴, 내가 laravel 5.2 동일한 쿼리를 작성해야합니다. 내 SQL 쿼리는 아래와 같습니다.laravel에서 SQL join 쿼리를 작성하는 방법 5.2

그리고 나는 또한 laravel에 글을 써 보았습니다. 아래에 쿼리가 있습니다.

DB::table('device as b') 
    ->join('eventdata as a', 'a.deviceID', '=', 'b.deviceID') 
    ->where('a.deviceID', '=', '$deviceID') 
    ->where('a.accountID', '=', '$accountID') 
    ->where('a.timestamp', '>=', '$dt1') 
    ->where('a.timestamp', '<=', '$dt2') 
    ->select('a.accountID', 'a.deviceID', 'b.description', 'a.timestamp', 
      'a.latitude', 'a.longitude', 'a.speed', 'a.heading', 'a.altitude', 'a.address', 'a.distanceKM as distance', 'a.odometerKM as odometer', 'a.IbatVolts', 'a.EbatVolts', 'a.ITempr', 'a.fuelLevel', 'a.inputState', 'a.IgnRuntime', 'GPSFixType', 'a.GPSPDOP', 'a.AlterType', 'a.speedLimitKPH', 'a.isTollRoad')->get(): 

이게 맞습니까? 아무도 저에게 말하고 정확한 질문을 쓸 수있게 도와 줄 수 있습니까?

$users = DB::table('users') 
      ->join('contacts', 'users.id', '=', 'contacts.user_id') 
      ->join('orders', 'users.id', '=', 'orders.user_id') 
      ->select('users.*', 'contacts.phone', 'orders.price') 
      ->get(); 

과 동일한를 사용 :

+0

귀하의 laravel 코드는 내부 조인을 수행 하겠지만, 당신이 쓴 쿼리는 크로스 조인을 사용합니다 .. 당신은 어떤 예기치 않은 결과를 얻고있다 당신이 그것을 시도 할 때? ([설명서에 대한 참조] (https://laravel.com/docs/5.2/queries#joins) – Terminus

+0

nop .. 완벽하게 작동합니다.하지만 알아야 할 점은 개발에 충분하다는 것입니까? –

답변

3

는 laravel 5.2의 구문은 가입 할 수 있습니다. 사용하여 원시 SQL 쿼리를 인쇄 할 수있는 것보다 경우에 당신이 어떤 문제에 직면하고있다 :

DB::enableQueryLog(); 

// Your query 

$queries = DB::getQueryLog(); 
print_r($queries); // it will print raw sql query in prepared statement style 
+0

ok .. 감사합니다 ...... –

관련 문제