在我的数据库中,我有32个表,并且大多数表都包含相同的i。如何删除在一个查询中只包含特定ID的整个数据库中的所有数据(我是指包含该特定id的每个表)。
DELETE * from (ALL TABLES) where id = 3;发布于 2016-09-25 02:48:03
问得好。查询可能是此问题的最佳解决方案。
我已经在下面给出了使用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");
}
?>或
再创建一个表,在其中为需要从所有表中删除的id创建每日条目,并且每天可能会从所有表中删除不同的id列表。
在下面,我创建了一个表ids_to_delete,其中指定了要删除的ids列表。
<?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`");
?>https://stackoverflow.com/questions/39674105
复制相似问题