簡體   English   中英

PHP mysql從所有表中刪除條目

[英]PHP mysql delete entry from all table

  • 我的數據庫中有85個表格(工作人員,訪問權限,培訓,通知等)
  • 所有表都有一個字段公共字段staff_id。 我面臨着刪除不需要的記錄的問題
  • 有什么辦法可以從工作人員id = xyz的85個表中刪除記錄

嘗試這個...

//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 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);
}

你是這個意思嗎

DELETE FROM `entries` WHERE staff_id="spiderman"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM