Answer the question
In order to leave comments, you need to log in
What is the best way to implement a category tree for an online store in SQL?
What is the best way to implement a list of categories with subcategories in a MySQL database for an online store?
Nesting level no more than 4:
Example: Household appliances -> Kitchen -> Coffee makers -> Capsule
The main thing you need is the speed of sampling from the database
Over time, subcategories can be added / removed , it should happen without hemorrhoids, what scheme would you recommend for use?
Adjacency List:
CREATE TABLE `category` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`parent_id` int(10) UNSIGNED NOT NULL
`name` varchar(255) NOT NULL,
`status` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`parent_id`)
);
CREATE TABLE `category` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`parent_id` int(10) UNSIGNED NOT NULL
`name` varchar(255) NOT NULL,
`left` int(10) UNSIGNED NOT NULL,
`right` int(10) UNSIGNED NOT NULL,
`level` int(10) UNSIGNED NOT NULL,
`status` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`parent_id`)
);
Answer the question
In order to leave comments, you need to log in
CREATE TABLE `category` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`parent_id` int(10) UNSIGNED NOT NULL,
`path_id` int(10) UNSIGNED NOT NULL,
`related` JSON,
`name` varchar(255) NOT NULL,
`status` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`parent_id`)
);
See how the ruby gem works https://github.com/stefankroes/ancestry
It stores all parents "11/23/145" as a string, as a result, LIKE queries are used instead of a bunch of recursive queries.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question