Home  >  Q&A  >  body text

I would like to ask someone about the problem of thinkphp operating database. How to keep the latest 20 entries and clear the redundant ones if they are larger than 20?

if($logNums >= 20) {

$logList = $tableLog->limit(20)->order("id", "asc")->select() ;

foreach($logList as $k => $v){

$condition[] = $v['id'];

}

$tableLog->where('id', 'in', $logList)->limit(20)->order("id", "asc")->delete();

}

I would like to ask someone about the problem of thinkphp operating database. How to keep the latest 20 entries and clear the redundant ones if they are larger than 20?

Thanks in advance for the answer..

P粉448732323P粉448732323932 days ago1047

reply all(1)I'll reply

  • autoload

    autoload2022-04-25 09:58:52

    Get the latest 20 data IDs in the database, then use whereNotIn() to filter them out, and then delete them

    reply
    0
  • Cancelreply