简体   繁体   English

PHP / MySQL批量删除

[英]PHP/MySQL Batch Delete

is there a way to batch up deletes for MySQL when using PHP (mysqli)? 有没有办法在使用PHP(mysqli)时批量删除MySQL?

With inserts you can do something like 使用插入,你可以做类似的事情

insert into tbl (...) values (...) (...) (...) ...

is there any equivalent for deletes? 删除是否有任何等价物?

delete from tbl where id IN (1,2,3)

OR 要么

delete from tbl where id = 1 OR id = 2 OR email = 'admin@example.com'

you can make sure you delete the right data by doing select first... 您可以先确定选择删除正确的数据...

ps: for the php, ps:对于php,

<?
$ids = array(1,2,3);

$sql = "DELETE FROM tbl WHERE id IN (" . implode(',', $ids) . ")";
?>

or 要么

<?
$deleteme = array("id = 1", "id = 2", "email = 'admin@example.com'");

$sql = "DELETE FROM tbl WHERE " . implode(' OR ', $deleteme);
?>

You can use the WHERE clausole. 您可以使用WHERE clausole。 For example: 例如:

DELETE FROM table WHERE Id in (0,1,...,n)

If I understood your question correctly, then: 如果我理解你的问题,那么:

  • It does not really depend on PHP, it's purely up to the SQL statement used 它并不真正依赖于PHP,它纯粹取决于所使用的SQL语句
  • Usually, you delete rows based ON WHERE X = Y condition. 通常,您根据ON WHERE X = Y条件删除行。 So if you want to delete multiple rows, you would just extend the condition to WHERE X = Y OR X = Z or even WHERE X IN (Y, Z, ..) 因此,如果要删除多行,只需将条件扩展为WHERE X = Y OR X = Z或甚至WHERE X IN (Y, Z, ..)

Use In clause in delete statement, this is one of the best methods and you can pass thousands of ids in IN clause. 在delete语句中使用In子句,这是最好的方法之一,您可以在IN子句中传递数千个ID。 I personally tested 25k ids and it's worked 我亲自测试了25k ID,它的工作原理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM