Answer the question
In order to leave comments, you need to log in
How to delete data from multiple tables in one query?
I have several hundred tables and I need to remove certain data from them. Tables are named table_1,table_2,table_3 and so on.
Here I find the necessary tables and the query itself what to delete.
SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE '%table_%'
DELETE
FROM `table_xx`
WHERE `id` = 0
DELETE
FROM (SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE '%table_%')
WHERE id = 0
Answer the question
In order to leave comments, you need to log in
SET @del := (SELECT CONCAT('DELETE FROM ', GROUP_CONCAT(TABLE_NAME SEPARATOR ' WHERE id=0; DELETE FROM ') , ';' )
AS statement FROM information_schema.TABLES WHERE TABLE_NAME LIKE '%table_%' );
PREPARE stmt FROM @del;
EXECUTE stmt;
Perhaps the "Execute Immediate" option will suit you. Read about him.
I'll take a closer look tonight and leave you a comment.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question