PHP mysql从所有表中删除条目 我的数据库中有85个表(员工、访问、培训、通知等) 所有表都有一个字段公共字段工作人员id.m面临删除不需要的记录的问题 是否有任何方法可以从staff id=xyz的85个表中删除记录
试试这个PHP mysql从所有表中删除条目 我的数据库中有85个表(员工、访问、培训、通知等) 所有表都有一个字段公共字段工作人员id.m面临删除不需要的记录的问题 是否有任何方法可以从staff id=xyz的85个表中删除记录,php,mysql,Php,Mysql,试试这个 //Loop through all tables set_time_limit(0); $res = mysqli_query($db,"SHOW TABLES"); while ($row = mysqli_fetch_row($res)) { $table = $row[0]; //Add created_at column if not exist, else alter the field $response = mysqli_query(
//Loop through all tables
set_time_limit(0);
$res = mysqli_query($db,"SHOW TABLES");
while ($row = mysqli_fetch_row($res)) {
$table = $row[0];
//Add created_at column if not exist, else alter the field
$response = mysqli_query($db,"DELETE FROM " . $table . " WHERE staff_id = xyz");
if ($response)
echo "Data deleted from " . $table;
}
你是说像这样吗
DELETE FROM `entries` WHERE staff_id="spiderman"
你可以这样做。您已按如下方式联接所有表。在这里,我只加入了上面给定的表格。在删除记录之前还要考虑外键约束
DELETE t1, t2, t3, t4 FROM
staff as t1
INNER JOIN access as t2 on t1.staff_id = t2.staff_id
INNER JOIN training as t3 on t1.staff_id=t3.staff_id
INNER JOIN notifications as t4 on t1.staff_id=t4.staff_id
WHERE t1.staff_id=xyz;
除非像这样尝试:
$tables = array("staff", "access", "training", "notifications");
foreach ($tables as $table) {
$query = "DELETE FROM $table WHERE staff_id=xyz";
mysqli_query($db, $query);
}
您的表是否具有引用完整性,因为您定义了所有外键?是的,所有表都具有引用完整性,并且所有表都通过staff_id连接……如果表之间的关系正确,请尝试级联删除:这样我必须创建t1、t2、t3。。。。。。高达85,m仍在继续添加新表…好的。继续。祝你好运