我正在尝试从“interviews”表中获取每个candidate_id 的最新记录。
我在 laravel 上使用 Eloquent 并且已经尝试过这种方法(有和没有 eloquent):
$candidates = DB::table('interviews')->select('interviews.*', 'i2.*')
->leftJoin('interviews as i2',
function ($join) {
$join->on('interviews.candidate_id', '=', 'i2.candidate_id');
$join->on('interviews.created_at', '<', 'i2.created_at');
}
)
->whereNull('i2.candidate_id')
->get();
我雄辩地尝试过这个:
$candidates = Interview::leftJoin('interviews as i2',
function ($join) {
$join->on('interviews.candidate_id', '=', 'i2.candidate_id');
$join->on('interviews.created_at', '<', 'i2.created_at');
}
)->whereNull('i2.candidate_id')
->get();
如果我更改get()
为toSql()
我有上图中显示的完全相同的查询,但在 laravel 上运行我总是得到这些结果(这使用第一种方法,使用查询生成器):
有人知道我为什么会得到这个结果吗?很难理解 laravel 正在执行与 HeidiSql 中相同的查询,但我得到了不同的结果:(
任何提示?
提前致谢!