Php Laravel 5.3多条件连接有计数的雄辩连接表
我对使用having count选择要显示的主表的左联接表有疑问,条件如下:Php Laravel 5.3多条件连接有计数的雄辩连接表,php,laravel,join,eloquent,having-clause,Php,Laravel,Join,Eloquent,Having Clause,我对使用having count选择要显示的主表的左联接表有疑问,条件如下: select b.id, b.location_name, b.box_identity from box b left join device d on b.id = d.box_id and d.deleted_at is null group by b.id, b.location_name, b.box_identity having count(d.
select b.id, b.location_name, b.box_identity
from box b
left join device d on b.id = d.box_id
and d.deleted_at is null
group by b.id, b.location_name, b.box_identity
having count(d.*) < COALESCE(b.device_slot, 3)
order by b.id desc
选择b.id、b.location\u name、b.box\u identity
从方框b
b.id=d.box\U id上的左连接设备d
d.deleted_at为空
按b.id、b.location\u name、b.box\u标识分组
具有计数(d*)<合并(b.设备\u插槽,3)
按b.id描述订购
我尝试过使用像这样雄辩的代码,但DB::raw在$join函数中不起作用
$boxes = Box::leftJoin('device', function($join) {
$join->on('box.id', '=', 'device.box_id');
$join->on(DB::raw('device.deleted_at is null'));
})
->select('box.id', 'box.box_identity', 'box.location_name')
->groupBy('box.id', 'box.box_identity', 'box.location_name')
->havingRaw('COUNT(device.*) < COALESCE(box.device_slot, 3)')
->orderBy('box.id', 'desc')
->get();
$Box=Box::leftJoin('device',function($join){
$join->on('box.id','=','device.box_id');
$join->on(DB::raw('device.deleted_at为null');
})
->选择('box.id'、'box.box\u identity'、'box.location\u name')
->groupBy('box.id'、'box.box\u identity'、'box.location\u name')
->havingRaw('COUNT(device.*)<合并(box.device_插槽,3)'
->orderBy('box.id','desc')
->get();
如何使用laravel eloquent实现此查询?提前谢谢 使用以下命令:
$boxes = Box::leftJoin('device', function($join) {
$join->on('box.id', '=', 'device.box_id');
$join->whereRaw('device.deleted_at is null');
})
您可以尝试
$join->whereRaw('device.deleted_at为null')是的,我想你的答案是对的。非常感谢,海当!一点也不,我的朋友。我认为应该使用$join->on('device.deleted_at','=',DB::raw('NULL');但结果仍然是无效的。安装了3个设备的框仍然显示,必须不显示。try DB::enableQueryLog();dd(DB::getQueryLog());最后。然后在此处发布查询您的查询将检查MySQL中的deleted\u at=“NULL”
字符串,而不是deleted\u at为NULL
。在这种情况下,必须使用原始查询
@BRjava是的,现在这是正确的,就像海当的答案一样