Laravel Query Builder无法正常运行

时间:2015-04-24 13:36:18

标签: php mysql laravel query-builder

我正在尝试使用laravel进行高级连接,如下所示:

$locations = DB::table('location')
        ->select('location.location_time',
                'heart_rate.heartrate'  
        )
        ->leftJoin('heart_rate' , function($join) use ($data) {
                $join->on('heart_rate.user_id', '=', DB::raw('1'))
                    ->where('heart_rate.time', '>',  DB::raw('(location.location_time - INTERVAL 5 SECOND)')) //   ->whereBetween('heart_rate.time', array(, DB::raw('location.location_time + 2 SECOND')));
                    ->where('heart_rate.time', '<',  DB::raw('(location.location_time + INTERVAL 5 SECOND)'));     
                })
        ->where('location.user_id', '=',  DB::raw('1'))
        ->whereBetween('location.location_time', array( "2015-04-24 00:00:00", "2015-04-25 23:59:59" ))
        ->get();

返回以下内容:[{"location_time":"2015-04-24 14:00:00","heartrate":null}]

当我让它转储查询并直接输入到mySQL中时,我得到以下结果:enter image description here,它应该是这样的,因为数据库中的表是:

表'heart_rate':enter image description here

表'位置':enter image description here

SQL Query i转储是

SELECT 
  `location`.`location_time`,
  `heart_rate`.`heartrate` 
FROM
  `location` 
  LEFT JOIN `heart_rate` 
    ON `heart_rate`.`user_id` = 1 
    AND `heart_rate`.`time` > (
      location.location_time - INTERVAL 5 SECOND
    ) 
    AND `heart_rate`.`time` < (
      location.location_time + INTERVAL 5 SECOND
    ) 
WHERE `location`.`user_id` = 1 
  AND `location`.`location_time` BETWEEN "2015-04-24 00:00:00" 
  AND "2015-04-25 23:59:59" 

0 个答案:

没有答案