I have a lots of products that we need to assign categories and a table, that contains keywords by which you can identify what goes where. There may be multiple keywords for one category, in this case the first match from top should be used.
I came up with this test case, which works as I need:
CREATE TABLE Products
(`name` varchar(30), `category_id` int, `filter` varchar (30))
;
INSERT INTO Products
(`name`)
VALUES
('Plate'),
('Mushrooms'),
('Mushroom soup in a plate'),
('Mushroom on a plate'),
('Tomato soup'),
('Sausage'),
('Soupcan')
;
CREATE TABLE Filters
(`filter` varchar(30), `category_name` varchar(30))
;
INSERT INTO Filters
(`filter`, `category_name`)
VALUES
('Soup', 'Meals'),
('Mushroom', 'Ingredients'),
('Plate', 'Containers')
;
CREATE TABLE Categories
(`id` int, `name` varchar(30))
;
INSERT INTO Categories
(`id`, `name`)
VALUES
(1, "Ingredients"),
(2, 'Containers'),
(3, 'Meals')
;
update Products
left join Filters
on Products.name LIKE CONCAT('%',Filters.filter,'%')
join Categories
on Filters.category_name = Categories.name
set Products.category_id = Categories.id, Products.filter = Filters.filter;
select Products.*, Categories.name as category_name from Products
left join Categories on Products.category_id = Categories.id
Category gets assigned by first match in "filters", just as I need.
But if I add another row to Filter table that has same category_id (for example try adding: ('Soupcan', 'Containers') to Filters), the results will not be the first match (or the last).
"Mushroom soup in a plate" must go to "Meals", because it matches first filter row "Soup".
But if I add 'Soupcan', 'Containers' row to Filters - "Mushroom soup in a plate" will now match "Mushroom" and get into "Ingredients" category.
I think the problem lies in sorting between first and second join, but I can't figure it out.
If you know a better approach to this task - feel free to offer. I just need to sort products by keyword matching, where topmost keyword matched will have the priority.