I have following legacy database setup:
CREATE TABLE `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`category_ids` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
)
in which category_ids is a string of category id’s separated by comas: 1, 10, 15, 6. Is there a way to convert this database to more conventional one (using three tables, one for storing relationships), using only SQL and no other scripts?
MySQL doesn’t have CROSS APPLY or recursive CTEs, which would be the simplest routes.
But you’re only dong this once, so you only need a quick hack.
First, find out the maximum number of items in the category list…
Then you can do something like this…
I haven’t tested it, but I’m assuming that
SUBSTRING_INDEX('last', ',', -1)returns'last'.I’m no MySQL expert, so this may not be optimal, but as a one time quick win thistype of structure should work…