我不是mysql的专家。我有一个包含500多个表的数据库,我想知道是否有方法在每个表中搜索某个记录,如果存在则从表中删除它。提前致谢
答案 0 :(得分: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');
答案 1 :(得分: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);
}
答案 2 :(得分: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);
}