我不是mysql专家。我有一个包含 500 多个表的数据库,我想知道是否有办法在每个表中搜索某个记录并从表中删除它(如果存在)。提前致谢
问问题
544 次
3 回答
1
查看您可以创建的以下存储过程:
CREATE PROCEDURE procDeleteAllTables()
BEGIN
DECLARE table_name VARCHAR(255);
DECLARE end_of_tables INT DEFAULT 0;
DECLARE cur CURSOR FOR
SELECT t.table_name
FROM information_schema.tables t
WHERE t.table_schema = DATABASE() AND t.table_type='BASE TABLE';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_of_tables = 1;
SET FOREIGN_KEY_CHECKS = 0;
OPEN cur;
tables_loop: LOOP
FETCH cur INTO table_name;
IF end_of_tables = 1 THEN
LEAVE tables_loop;
END IF;
SET @s = CONCAT('DELETE FROM ' , table_name);
PREPARE stmt FROM @s;
EXECUTE stmt;
END LOOP;
CLOSE cur;
SET FOREIGN_KEY_CHECKS = 1;
END
如果您需要为特定记录插入检查,可以通过以下方式添加 WHERE 子句:
SET @s = CONCAT(CONCAT('DELETE FROM ' , table_name), ' WHERE somefield = 1');
于 2012-09-23T15:37:16.090 回答
1
类似的东西。根据您的需要进行更改。
$tables = mysql_query('show tables', $conn);
foreach( $acc_array as $acc)
foreach( $tables as $v) {
$result1 = mysql_fetch_assoc(mysql_query("select account_num from $v where account_num = '$acc'" , $conn));
if( !empty($result1) )
$result2 = mysql_query("delete from $v where account_num = '$acc'" , $conn);
}
于 2012-09-23T15:40:09.493 回答
0
$sql = "SHOW TABLES FROM dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
$tablename=$row[0];
$sql="DELETE FROM $tablename WHERE name='ddddd'";
echo "Table: {$sql}\n";
$res=mysql_query($sql);
}
于 2012-09-23T17:24:28.573 回答