如果 feildx=123,则删除数据库中任何表中的任何记录 [英] DELETING any record in any table in the database if feildx=123
问题描述
是否有一个 mysql statemnet 遍历数据库中的每个表并说如果 applicationid =123 或 schoolid=123 或 familyid = 123 删除整个记录?我需要编写一个 php 脚本来执行此操作.
is there a mysql statemnet that goes through every table in a data base and says if applicationid =123 or schoolid=123 or familyid = 123 DELETE THE WHOLE record? i need to write a php script that will do this.
推荐答案
SELECT TABLE_NAME, GROUP_CONCAT(DISTINCT COLUMN_NAME SEPARATOR ',') AS columns
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'yourdatabasename'
AND COLUMN_NAME IN ('applicationid', 'schoolid', 'familyid')
GROUP BY TABLE_NAME
结果将是每个表的数组,以及它具有的列(仅来自`applicationid、schoolid、familyid 的集合)作为逗号分隔的字段.
The result will be an array of each table, and the columns that it has (only from the set of `applicationid, schoolid, familyid) as a comma separated field.
foreach ($results as $result) {
$cols = explode(',', $result['columns']);
foreach ($cols as &$col) {
$col .= ' = 123';
}
$sql = 'DELETE FROM '. $result['TABLE_NAME'].
' WHERE ' . implode(' OR ', $cols);
}
这将为每个表生成一个查询,例如:
That'll generate a query for each table like:
DELETE FROM table1 WHERE applicationid = 123 OR schoolid = 123
它只会包含表格中的字段...
And it will only include the fields within the tables...
这篇关于如果 feildx=123,则删除数据库中任何表中的任何记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!