$id=array(1,3,5);
$sql="delete * from tablename where id = ".$id;
现在我要删除$ id为1,3和5的mysql中的所有记录。
答案 0 :(得分:2)
DELETE FROM `tablename` WHERE id IN (1, 3, 5);
使用PDO:
$ids = array(1, 3, 5);
/* Create a string for the parameter placeholders filled to the number of params */
$place_holders = implode(',', array_fill(0, count($ids), '?'));
/*
This prepares the statement with enough unnamed placeholders for every value
in our $ids array. The values of the $ids array are then bound to the
placeholders in the prepared statement when the statement is executed.
*/
$stmt = $dbh->prepare("DELETE FROM tablename WHERE id IN (" . $place_holders . ")");
$stmt->execute($ids);
答案 1 :(得分:0)
在mysql中尝试以下:
DELETE FROM tablename WHERE id IN (1, 3, 5)
https://dev.mysql.com/doc/refman/4.1/en/delete.html
https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in
答案 2 :(得分:0)
使用id删除:
DELETE FROM `tablename` WHERE id = 1;
带有IN子句的:
DELETE FROM `tablename` WHERE id IN (1, 3, 5);
删除指定表格中的所有内容
TRUNCATE table `tablename`;
使用给定的元素数组进行删除:
foreach($id as $i) {
$sql="delete * from tablename where id = ".$i;
}
答案 3 :(得分:0)
$id=array(1,3,5);
$ids = implode(",",$id);
$sql="delete * from tablename where id IN (".$ids.")";
使用上面的代码。
答案 4 :(得分:0)
首先需要将数组转换为普通字符串
$ids = array(1, 3, 5);
$strngId = implode(",",$ids);
现在使用此变量可以编写删除查询。
$sql = "Delete * from tablename where idfield IN (".$strngId.")";