备注:SELECT `log_id`,count(*) as count FROM `dp_im_chat` GROUP BY `log_id` HAVING count>1 public function clear_trush_data() { $data=db('im_chat')->field('log_id,count(*) as count') ->group('log_id')->having('count>1')->select(); dump($data); foreach ($data as $k=>$v){ $check=db('im_chat')->where('log_id',$v['log_id'])->value('id'); $clear=db('im_chat')->where('log_id',$v['log_id'])->where('id','neq',$check)->delete(); dump(db('')->getLastSql()); } }