Laravel 数据去重

public function timingApplyBorrow()
    {
        //截断表数据
        YunUserBorrowList::truncate();

        YunUserEntry::select('id', 'name', 'company_id', 'id_no', 'id_no')->chunk(100, function ($data) {
            $array = [];
            $arr = [];
            foreach ($data as $value) {
                $arr['user_id'] = $value->id;
                $arr['name'] = $value->name;
                $arr['borrow_no'] = $this->is_allow;
                $arr['id_no'] = $value->id_no ?? '';
                $arr['company_name'] = $this->yunCompany($value->company_id);
                $array[] = $arr;
            }

            YunUserBorrowList::insert($array);
        });
        $sql = 'DELETE FROM yun_user_borrow_lists WHERE id_no IN(SELECT id_no FROM (SELECT id_no FROM yun_user_borrow_lists
                GROUP BY id_no HAVING COUNT(id_no) > 1) a) AND id NOT IN(SELECT * FROM (SELECT MIN(id) FROM yun_user_borrow_lists
                GROUP BY id_no HAVING COUNT(id_no) > 1) b)';
        //数据去重
        DB::delete($sql);
        return Helper::jsonSuccessData([], '申请成功,请等候审核');

    }
posted @ 2020-12-26 10:59  心之所依  阅读(2531)  评论(0编辑  收藏  举报