在 laravel 中跳过数据库中的重复数据
skip duplicate data from database in laravel
我只想跳过数据库中的重复数据,我的外键重复所以我只想跳过重复条目并只获取最新的
我的控制器就是这样
$old=DB::table('tabl-1')
->leftJoin('tbl-2','tabl1.id','=','tabl2.tabl1_id')
->whereDate('created_at','<' ,Carbon::today())
->select('tbl1.name as name','tbl1.class as class','tabl2.table1_id')
->distinct('tabl2.table1_id')
->get()
->toArray();
DB::table('first')->leftJoin('second', 'first.id', '=', 'second.first_id')
->whereDate('first.created_at', '<', Carbon::today())
->select('first.name as name', 'first.class as class', 'second.first_id')
->groupBy('second.first_id')->get()->toArray();
试试 groupBy,因为 distinct() 里面没有参数
我只想跳过数据库中的重复数据,我的外键重复所以我只想跳过重复条目并只获取最新的 我的控制器就是这样
$old=DB::table('tabl-1')
->leftJoin('tbl-2','tabl1.id','=','tabl2.tabl1_id')
->whereDate('created_at','<' ,Carbon::today())
->select('tbl1.name as name','tbl1.class as class','tabl2.table1_id')
->distinct('tabl2.table1_id')
->get()
->toArray();
DB::table('first')->leftJoin('second', 'first.id', '=', 'second.first_id')
->whereDate('first.created_at', '<', Carbon::today())
->select('first.name as name', 'first.class as class', 'second.first_id')
->groupBy('second.first_id')->get()->toArray();
试试 groupBy,因为 distinct() 里面没有参数