在我的数据库中,我有32个表,大多数表包含相同的ID。如何删除整个数据库中仅包含一个查询中特定ID的所有数据(我的意思是每个包含该特定ID的表)。
DELETE * from (ALL TABLES) where id = 3;
答案 0 :(得分:2)
好问题。查询可能是最佳解决方案。
我已在下面使用PHP作为您指定的PHP标记提供解决方案
<?php
require("db_connect.php");
$tables = mysqli_query($con, "SHOW TABLES FROM dbname");
while ($row = mysqli_fetch_assoc($tables)) {
$table_name = $row["Tables_in_dbname"];
mysqli_query($con, "DELETE FROM $table_name WHERE `id`=3");
}
?>
OR
再创建一个表格,您可以在其中为id
制作需要从所有表格中删除的每日条目,并且每天都可以从所有表格中获取要删除的id
的不同列表。
下面我已经创建了一个表ids_to_delete
,其中我指定了要删除的ID列表。
<?php
require("db_connect.php");
//get ids from table where specified ids to be deleted
$ids = mysqli_query($con, "SELECT `id` FROM `ids_to_delete`");
$id_list = '';
//create ids list like 1,4,3,9,5,6,...
while ($row_id = mysqli_fetch_assoc($tables)) {
$id_list .= $row_id['id'] . ',';
}
$id_list = trim($id_list, ',');
$tables = mysqli_query($con, "SHOW TABLES FROM dbname");
while ($row = mysqli_fetch_assoc($tables)) {
$table_name = $row["Tables_in_dbname"];
mysqli_query($con, "DELETE FROM $table_name WHERE `id` IN ($id_list)");
}
//clear the ids_to_delete table
mysqli_query($con,"DELETE FROM `ids_to_delete`");
?>