mojobullfrog
mojobullfrog

Reputation: 189

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

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;

Upvotes: 0

Views: 37

Answers (1)

ProDec
ProDec

Reputation: 5420

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
;

Upvotes: 1

Related Questions