Skip to content

Instantly share code, notes, and snippets.

@colindensem
Forked from kleinron/drop_many_tables.sql
Created August 18, 2016 05:24
Show Gist options
  • Save colindensem/f277467b451abac5e25e56f71ab79430 to your computer and use it in GitHub Desktop.
Save colindensem/f277467b451abac5e25e56f71ab79430 to your computer and use it in GitHub Desktop.
MySQL bulk drop table where table like... See the question and the answers: http://stackoverflow.com/q/11053116/17772
SET @temp_statement = NULL;
SELECT
GROUP_CONCAT(table_schema, '.`', table_name, '`') INTO @temp_statement
FROM
(
SELECT
table_schema, table_name
FROM
information_schema.tables
WHERE
table_schema = 'db_name_goes_here' AND table_name LIKE 'table_base_name_here_%'
LIMIT 10 -- a limit to avoid exceeding group_concat_max_len
) JUST_A_TEMP_NAME;
-- up to this point, @temp_statement holds something like this:
-- mydb.`table1`,mydb.`table2`,...,mydb.`tableN`
SET @temp_statement = CONCAT('DROP TABLE ', @temp_statement);
SELECT @temp_statement; -- let's see the SQL statement before executing it
PREPARE stmt1 FROM @temp_statement;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET @temp_statement = NULL; -- clean up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment