Laravel 加入限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39365935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel join with limit
提问by Dineshh Bhardwaj
I have the following working for laravel paginate
我有以下工作为 laravel paginate
$query = DB::table('clients')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', 'clients.username', '=', 'recharge.soldto');
I want to join some values from two tables radusergroup and recharge. radusergroup always return one row as it has only one row stored whereas recharge table return multiple rows. I want only one row returned from recharge table which is latest entry.
我想从两个表 radusergroup 中加入一些值并充值。radusergroup 总是返回一行,因为它只存储了一行,而 recharge 表返回多行。我只希望从最新条目的充值表返回一行。
Right now its return all the possible rows from recharge table and showing it on paginated view.
现在它从充值表中返回所有可能的行并在分页视图中显示它。
回答by Vahe Galstyan
This is Laravel Official documentation
DB::table('clinets')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', function ($leftJoin) {
$leftJoin->on('clients.username', '=', 'recharge.soldto')
->where('recharge.create_date', '=', DB::raw("(select max(`create_date`) from recharge)"));
})
->get();
this is the case if you have create_date column in your table, if you haven't got it I strongly recommend to create that column.
如果您的表中有 create_date 列,就是这种情况,如果您还没有,我强烈建议您创建该列。