[英]MySQL Update rows with double left join, limiting first match
I have three tables ( SQLFiddle with tables created ) 我有三个表( SQLFiddle与表创建 )
Orange text is what I need to get by comparing Products.name with Filters.filter. 通过比较Products.name和Filters.filter,我需要获得橙色文本。
I figured out that substring match can be done like this: 我发现子串匹配可以像这样完成:
on Products.name LIKE CONCAT('%',Filters.filter,'%');
I only need the first filter match to be used. 我只需要使用第一个过滤器匹配。 So "Mushroom soup" will match "Soup" and not "Mushroom". 所以“蘑菇汤”将匹配“汤”而不是“蘑菇”。
What is best approach for this task? 什么是这项任务的最佳方法?
A tested SQLFiddle link, if possible, please. 如果可能的话,请测试一个经过测试的SQLFiddle链接。
What I tried: ( feel free to skip everything below ) 我尝试了什么:( 随意跳过下面的所有内容 )
Tried double join: 尝试双重加入:
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;
But this doesn't result in first match being used, because first join returned all filter matches (multiple lines per 1 product), and after second join categories are from last match. 但这不会导致使用第一个匹配,因为第一个连接返回所有过滤器匹配(每个产品多个行),第二个连接类别来自最后一个匹配。
For example: Mushroom soup got into "Mushroom-Ingredients" filter\\category, instead of "Soup\\Meals". 例如:蘑菇汤进入“蘑菇成分”过滤器\\类别,而不是“汤\\餐”。
Also tried join + order by: 还试过加入+订单:
select Products.name, Filters.*
from Products
left join Filters on Products.name LIKE CONCAT('%',Filters.filter,'%')
group by Products.name;
This returns the first match per product as I needed, but I can't do a second left-join in the same query, neither can I get "group by" to work after "update" function instead of "select". 这将返回我需要的每个产品的第一个匹配,但我不能在同一个查询中执行第二个左连接,也不能在“更新”功能而不是“选择”之后“分组”工作。
For when sqlfiddle collapses: 当sqlfiddle崩溃时:
CREATE TABLE Products
(
`name` varchar(30),
`category_name` varchar (30),
`category_id` int
);
INSERT INTO Products (`name`)
VALUES ('Mushrooms'), ('Can of mushrooms'),
('Can of soup'), ('Mushroom soup'),
('Tomato soup'), ('Tomato');
CREATE TABLE Filters
(
`filter` varchar(30),
`category_name` varchar(30)
);
INSERT INTO Filters (`filter`, `category_name`)
VALUES ('Can of', 'Canned food'), ('Soup', 'Meals'),
('Mushroom', 'Ingredients'), ('Tomato', 'Ingredients');
CREATE TABLE Categories
(
`id` int,
`name` varchar(30)
);
INSERT INTO Categories (`id`, `name`)
VALUES (1, "Canned food"), (2, 'Ingredients'), (3, 'Meals');
You made the task quite easy with your sqlfiddle as well as your attempt to solve the problem with Select query. 您使用sqlfiddle以及尝试使用Select查询解决问题时,您可以轻松完成任务。
It works the way you want I guess and all I need to do is to add one more left join with category table (IDK why you were unable to join Category as it's working properly). 它按照你想要的方式工作,我需要做的就是在类别表中添加一个左连接(IDK为什么你无法加入Category,因为它正常工作)。
So. 所以。 I've edited your select query as follows: 我编辑了您的选择查询,如下所示:
select Products.name, Filters.*,Categories.id from Products
left join Filters
on Products.name LIKE CONCAT('%',Filters.filter,'%')
left join Categories
on Categories.name = Filters.category_name
GROUP BY Products.name;
You will get the results you want with this query. 您将通过此查询获得所需的结果。
Now, in order to update Products
table with the result of this query, you can do following: 现在,为了使用此查询的结果更新Products
表,您可以执行以下操作:
update Products
left join Filters
on Products.name LIKE CONCAT('%',Filters.filter,'%')
left join Categories
on Categories.name = Filters.category_name
set Products.category_name = Filters.category_name,
Products.category_id = Categories.id;
Click here for Working Demo 单击此处查看工作演示
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.