I have same table with json field:
CREATE TABLE IF NOT EXISTS `collections` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`items` json DEFAULT NULL,
`title` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO `collections` SET `id` = 1, `items`='{"0": {"id": 12967, "color": 17}, "1": {"id": 12920, "color": 69}, "2": {"id": 12310, "color": 24}, "3": {"id": 12261, "color": 17}, "4": {"id": 11874, "color": 17}}', `title`= 'First';
I want split items from collections and move item to new table:
CREATE TABLE IF NOT EXISTS `collection_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`collection` int(11) DEFAULT NULL,
`item` int(11) DEFAULT NULL,
`color` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO `collections_items` SET `collection`=1, `item`=12967, `color`=17;
INSERT INTO `collections_items` SET `collection`=1, `item`=12920, `color`=69;
Note: I don't want to use any scripts beyond MySQL (php, etc). If I use:
SELECT items, JSON_EXTRACT(items, '$.*.id') ids, JSON_EXTRACT(items, '$.*.color') colors
FROM site_catalog_collections
WHERE id=1;
It return array as field:
ids
[12967, 12920, 12310, 12261, 11874]
colors
[17, 69, 24, 17, 17]
How it insert to collection_items?