SQL/Laravel - 为什么我的查询 returns 是一个空集合?
SQL/Laravel - Why does my query returns an empty collection?
我的 Laravel Query Builder returns 一个空集合,而 SQL 字符串本身 returns 在 phpmyadmin 中执行时的正确记录。
这是我的代码:
$times = Time::join(\DB::raw('(SELECT `ath_id`, `stroke_id`, MIN(time) AS time FROM times GROUP BY ath_id, stroke_id) b'), function($join) {
$join->on('times.ath_id', '=', 'b.ath_id')
->where('times.stroke_id', '=', 'b.stroke_id')
->where('times.time', '=', 'b.time');
})
->where('times.ath_id', '=', $id)
->orderBy('times.stroke_id', 'ASC')
->orderBy('times.date', 'DESC');
dd($times->get());
下面是在 phpmyadmin 中运行的 sql,但不适用于 laravel 查询生成器。另外,这是使用dd($times->toSql());
时返回的SQL字符串(其中$times->getBindings()
returns['b.stroke_id', 'b.time', '4298584']
填写问号?
)
SELECT * FROM `times`
INNER JOIN (SELECT `ath_id`, `stroke_id`, MIN(time) AS time
FROM times GROUP BY ath_id, stroke_id) b
ON `times`.`ath_id` = `b`.`ath_id`
ADN `times`.`stroke_id` = b.stroke_id -- ?
AND `times`.`time` = b.time -- ?
WHERE `times`.`ath_id` = 4298584 -- ?
ORDER BY `times`.`stroke_id` asc, `times`.`date` desc
有点"tricky"。您在 phpmyadmin 中执行的查询确实是正确的。但是,Laravel 使用 where()
和 on()
的方式不同。
使用 where()
值和 on()
处理列。
$times = Time::join(\DB::raw('(SELECT `ath_id`, `stroke_id`, MIN(time) AS time FROM times GROUP BY ath_id, stroke_id) b'), function($join) {
$join->on('times.ath_id', '=', 'b.ath_id')
->on('times.stroke_id', '=', 'b.stroke_id')
->on('times.time', '=', 'b.time');
})
->where('times.ath_id', '=', $id)
->orderBy('times.stroke_id', 'ASC')
->orderBy('times.date', 'DESC');
文档:https://laravel.com/docs/5.4/queries,部分 (CTRL+F):高级联接
If you would like to use a "where" style clause on your joins, you may use the where
and orWhere
methods on a join. Instead of comparing two columns, these methods will compare the column against a value.
为了更清楚一点:
$join->on('times.ath_id', '=', 'b.auth_id')->where('times.stroke_id', '=','b.stroke_id');
结果:
JOIN on `times`.`ath_id` = `b`.`auth_id` WHERE `times`.`stroke_id` = 'b.stroke_id' -- AS STRING
当 toSql()
返回您的查询并且您假设 Laravel
知道:
['b.stroke_id', 'b.time', '4298584']
前两个绑定绑定是列。但是 where()
认为它们只是字符串。
我的 Laravel Query Builder returns 一个空集合,而 SQL 字符串本身 returns 在 phpmyadmin 中执行时的正确记录。
这是我的代码:
$times = Time::join(\DB::raw('(SELECT `ath_id`, `stroke_id`, MIN(time) AS time FROM times GROUP BY ath_id, stroke_id) b'), function($join) {
$join->on('times.ath_id', '=', 'b.ath_id')
->where('times.stroke_id', '=', 'b.stroke_id')
->where('times.time', '=', 'b.time');
})
->where('times.ath_id', '=', $id)
->orderBy('times.stroke_id', 'ASC')
->orderBy('times.date', 'DESC');
dd($times->get());
下面是在 phpmyadmin 中运行的 sql,但不适用于 laravel 查询生成器。另外,这是使用dd($times->toSql());
时返回的SQL字符串(其中$times->getBindings()
returns['b.stroke_id', 'b.time', '4298584']
填写问号?
)
SELECT * FROM `times`
INNER JOIN (SELECT `ath_id`, `stroke_id`, MIN(time) AS time
FROM times GROUP BY ath_id, stroke_id) b
ON `times`.`ath_id` = `b`.`ath_id`
ADN `times`.`stroke_id` = b.stroke_id -- ?
AND `times`.`time` = b.time -- ?
WHERE `times`.`ath_id` = 4298584 -- ?
ORDER BY `times`.`stroke_id` asc, `times`.`date` desc
有点"tricky"。您在 phpmyadmin 中执行的查询确实是正确的。但是,Laravel 使用 where()
和 on()
的方式不同。
使用 where()
值和 on()
处理列。
$times = Time::join(\DB::raw('(SELECT `ath_id`, `stroke_id`, MIN(time) AS time FROM times GROUP BY ath_id, stroke_id) b'), function($join) {
$join->on('times.ath_id', '=', 'b.ath_id')
->on('times.stroke_id', '=', 'b.stroke_id')
->on('times.time', '=', 'b.time');
})
->where('times.ath_id', '=', $id)
->orderBy('times.stroke_id', 'ASC')
->orderBy('times.date', 'DESC');
文档:https://laravel.com/docs/5.4/queries,部分 (CTRL+F):高级联接
If you would like to use a "where" style clause on your joins, you may use the
where
andorWhere
methods on a join. Instead of comparing two columns, these methods will compare the column against a value.
为了更清楚一点:
$join->on('times.ath_id', '=', 'b.auth_id')->where('times.stroke_id', '=','b.stroke_id');
结果:
JOIN on `times`.`ath_id` = `b`.`auth_id` WHERE `times`.`stroke_id` = 'b.stroke_id' -- AS STRING
当 toSql()
返回您的查询并且您假设 Laravel
知道:
['b.stroke_id', 'b.time', '4298584']
前两个绑定绑定是列。但是 where()
认为它们只是字符串。