Home > Blockchain >  How do I copy a table if a specific condition is met?
How do I copy a table if a specific condition is met?

Time:11-10

I can determine whether a condition is met by using the following:

SELECT id FROM admin WHERE firstname='John' INTO @tmp
SELECT IF (@tmp >= 1, "success", "fail")

But how would I create a copy of the table if the same condition is met? eg.

CREATE TABLE admin_copy LIKE admin
INSERT admin_copy
SELECT * FROM admin;

CodePudding user response:

This might work for you.

SET @create_table := IF(@tmp >= 1, 'CREATE TABLE admin_copy LIKE admin', 'SELECT 0')
;
PREPARE stmt_create FROM @create_table
;
EXECUTE stmt_create
;
DEALLOCATE PREPARE stmt_create
;
  • Related