selectRaw('max(id) as id_max,site_id') ->groupBy('site_id') ->pluck('id_max', 'site_id')->toArray(); //3个月内没有拓展过 $list = PrKeywordExtend::query()->whereIn('id', $maxIds)->get(); $ids = []; $date = date("Ym", strtotime("last day of -3 month", strtotime(date("Ym")))); foreach ($list as $value) { if ($value->ym <= $date) { $ids[] = $value->site_id; } } Site::query() ->whereIn('id', $ids) ->whereIn('status', [2, 3]) ->update(['keyword_memo' => '']); //从未拓展过 $date = date("Y-m-d", strtotime("last day of -3 month", strtotime(date("Y-m-d")))); $id = Site::query() ->where('online_at', '<=', $date) ->whereIn('status', [2, 3]) ->pluck('id')->toArray(); $ids1 = []; foreach ($id as $value) { $result = PrKeywordExtend::query()->where('site_id', $value)->first(); if (empty($result)) { $ids1[] = $value; } } Site::query() ->whereIn('id', $ids1) ->whereIn('status', [2, 3]) ->update(['keyword_memo' => '']); } }