table('ckb_custom_handle_log')->select('custom_no')->distinct()->where('type', 11)->get()->pluck('custom_no')->toArray(); fputcsv($handle, ['客户编号', '间隔时间']); foreach (array_chunk($custom_no, 1000) as $values) { $res = DB::connection('vd_old')->table('ckb_custom_handle_log')->whereIn('custom_no', $values)->whereIn('type', [10, 11])->orderBy('valid_start_time')->get()->groupBy('custom_no'); foreach ($res as $no => $group) { echo $no . PHP_EOL; for ($i=1; $i < count($group); $i++) { $item1 = $group[$i - 1]; $item2 = $group[$i]; $interval = Carbon::createFromTimestamp(intval($item2->valid_start_time))->diffInMonths(Carbon::createFromTimestamp(intval($item1->valid_end_time))); fputcsv($handle, [$no, $interval]); } } } fclose($handle); dd(1);