php Laravel -如何限制一个记录

628mspwn  于 2023-03-28  发布在  PHP
关注(0)|答案(2)|浏览(134)

无法从表中获取最后一条记录,我的表中有许多记录,我只需要获取最后一条记录。
我有这个疑问:
我需要获取上次连接查询记录

->leftJoin('vtask as avt', function($q) {
                    $q->on('avt.vehicle_id', '=', 't.vehicle_id')
                        ->on('avt.shift_start', '<=', 'dqe.time')->orderBy('avt.shift_start', 'desc');
                })
                ->leftJoin('vreplace as avr', function($q) {
                    $q->on('avr.vehicle_id', '=', 't.vehicle_id')
                        ->on(DB::raw("avr.time"), '>=', 'dqe.time')->orderBy('avr.time', 'desc');
                })
DB::table('dqevent','dqe')
                ->join('transport as t','t.id', '=', 'dqe.transport')
                ->join('vehicle as v','v.id', '=', 't.vehicle_id')
                ->leftJoin('vtask as avt', function($q) {
                    $q->on('avt.vehicle_id', '=', 't.vehicle_id')
                        ->on('avt.shift_start', '<=', 'dqe.time')->orderBy('avt.shift_start', 'desc');
                })
                ->leftJoin('vreplace as avr', function($q) {
                    $q->on('avr.vehicle_id', '=', 't.vehicle_id')
                        ->on(DB::raw("avr.time"), '>=', 'dqe.time')->orderBy('avr.time', 'desc');
                })
                ->leftJoin('ar as r','r.id', '=', DB::raw('COALESCE(avt.route_id, avr.route_id)'))
                ->leftJoin('driver as ad', 'ad.id', '=', DB::raw('COALESCE(avt.driver_id, avr.driver_id)'))
                ->selectRaw(
                    "dqe.id,
                    r.short_name,
                    dqe.lat,
                    dqe.lon
                ")
                ->orderBy('dqe.id','desc')->paginate(15);

从我的表中获取最后一条记录:)

vaqhlq81

vaqhlq811#

要从“dqevent”表返回最后一条记录,可以使用ORDER BYLIMITfirst修改查询:

DB::table('dqevent','dqe')
    ->join('transport as t','t.id', '=', 'dqe.transport')
    ->join('vehicle as v','v.id', '=', 't.vehicle_id')
    ->leftJoin('vtask as avt', function($q) {
        $q->on('avt.vehicle_id', '=', 't.vehicle_id')
            ->on('avt.shift_start', '<=', 'dqe.time')->orderBy('avt.shift_start', 'desc');
    })
    ->leftJoin('vreplace as avr', function($q) {
        $q->on('avr.vehicle_id', '=', 't.vehicle_id')
            ->on(DB::raw("avr.time"), '>=', 'dqe.time')->orderBy('avr.time', 'desc');
    })
    ->leftJoin('ar as r','r.id', '=', DB::raw('COALESCE(avt.route_id, avr.route_id)'))
    ->leftJoin('driver as ad', 'ad.id', '=', DB::raw('COALESCE(avt.driver_id, avr.driver_id)'))
    ->selectRaw(
        "dqe.id,
        r.short_name,
        dqe.lat,
        dqe.lon
    ")
    ->orderBy('dqe.id','desc')
    ->limit(1)
    ->first();

您可以阅读PostgreSQL示例中的SQL语法规则(从SQL99 STD的Angular 来看,几乎与MySQL,Oracle和MS SQL相同)。

SELECT with WHERE filtration
LIMIT and OFFSET pagination
ORDER BY ASC/DESC sorting
GROUP BY and HAVING aggregation
UNION / INTERSECT / EXCEPT combination
JOIN joiningmore joining
我知道你可能会使用mybatis,doctrine orm,linq,yalinqo,timetoogo pinq,hibernate + spring data jpa,jooq或laravel查询构建器框架,但它们只是SQL99子方言的语法糖。

brtdzjyr

brtdzjyr2#

我更改了代码并在表中插入了新索引
迁移文件

$table->index(['driver_id', 'vehicle_id', 'shift_start', 'shift_end', 'route_id']);

控制器

$avt = DB::raw("(select max(avt.id) from vtask  avt where avt.vehicle_id = t.vehicle_id and avt.shift_start <= dqe.time)");

->leftJoin('vreplace as avr', function($q) {
                        $q->on('avr.vehicle_id', '=', 't.vehicle_id')
                            ->on(DB::raw("avr.time"), '>=', 'dqe.time')->orderBy('avr.time', 'desc');
     ->on('avt.id', '=', $avt)
                    })

相关问题