Ashley
Ashley

Reputation: 5947

mysql while loop; select and insert

I'm trying to move enums from a table into their own table, the easiest way would be to run an SQL query which I figured I might use a WHILE loop for, but having never used one before the below obviously doesn't work. Is it possible to do something as the following:

WHILE (SET @cat = (SELECT DISTINCT `category` FROM `tablename`))
DO
  BEGIN
   INSERT INTO `categories` (title) VALUES (@cat);
  END;
END WHILE;

Upvotes: 0

Views: 5148

Answers (1)

silly
silly

Reputation: 7887

why do you dont try to insert the categories direct like this:

INSERT INTO `categories`
(
    title
)
SELECT DISTINCT
    `category`
FROM `tablename`

Upvotes: 5

Related Questions