我想在laravel 5之后构建查询
SELECT * FROM tbl WHERE updated_at IN (
SELECT max(updated_at) FROM tbl
WHERE created_at BETWEEN $begin_time AND $end_time
GROUP BY ip_address)
我的代码:
$sqlStr = self::select('*')
->whereIn('updated_at', function($query){
return $query->select(self::raw('max(updated_at)'))
->whereRaw('created_at >= $begin_time)
->whereRaw('created_at <= $end_time)
->groupBy('ip_address');
})->toSql();
var_dump(($sqlStr));die;
错误信息:
Grammar.php第58行中的ErrorException:
strtolower()期望参数1为字符串,给定对象
请帮帮我。
答案 0 :(得分:0)
当您编写生成self::raw('max(updated_at)')
SELECT max(updated_at) FROM tbl
的{{1}}时,它已经在select命令中。以下代码将起作用:
$sqlStr = self::select('*')
->whereIn('updated_at', function($query) {
$query->selectRaw('max(updated_at)')
->from('tbl')
->whereRaw('created_at >= $begin_time')
->whereRaw('created_at <= $end_time')
->groupBy('ip_address');
})->toSql();
答案 1 :(得分:0)
或者这也应该有效:
DB::select(
'SELECT * FROM tbl WHERE updated_at IN (' .
'SELECT max(updated_at) FROM tbl ' .
'WHERE created_at BETWEEN :begin_time AND :end_time ' .
'GROUP BY ip_address '
')',
[
'begin_time' => $begin_time,
'end_time' => $end_time
]
);