MySQL使用双左连接更新行,限制首次匹配 [英] MySQL Update rows with double left join, limiting first match

查看:164
本文介绍了MySQL使用双左连接更新行,限制首次匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三张表格(



Orange文本是我需要通过比较Products.name和过滤器.filter。



我发现子字符串匹配可以这样完成:

  on Products.name LIKE CONCAT('%',Filters.filter,'%'); 

我只需要使用第一个过滤器匹配。因此,蘑菇汤将匹配汤而不是蘑菇。



这项任务的最佳方法是什么?

请尝试使用SQLFiddle链接。



我尝试过:(可随意跳过下面的所有内容) p>

试过双连接:

 更新产品
left join过滤器on Products.name LIKE CONCAT('%',Filters.filter,'%')
加入类别Filters.category_name = Categories.name
set Products.category_id = Categories.id,Products.filter = Filters.filter;

但这不会导致第一个匹配被使用,因为第一个加入返回的所有过滤器匹配(每个产品多行),第二个加入类别来自上一次匹配。 例如:蘑菇汤进入蘑菇配料过滤器类别,而不是Soup \ Meals。

还尝试加入+排序:

  select Products.name,Filters。* 
from Products
left join Products.name上的过滤器LIKE CONCAT('%',Filters .filter,'%')
group by Products.name;

这会返回每个产品的第一个匹配项,但我不能再执行第二个左侧匹配项,加入相同的查询,我也不能通过更新功能而不是select来获得group by。

当sqlfiddle崩溃时:

  CREATE TABLE产品

`name`varchar(30),$ b $``category_name` varchar 30),
`category_id` int
);

插入商品(`name`)
价值('蘑菇'),('罐头蘑菇'),
('罐头汤'),('蘑菇汤'),
('番茄汤'),('番茄');

CREATE TABLE过滤器

`filter` varchar(30),$ b $``category_name` varchar(30)
);

INSERT INTO过滤器(`filter`,`category_name`)
VALUES('Can of','Canned food'),('Soup','Meals'),
('蘑菇','配料'),('番茄','配料');

CREATE TABLE类别

`id` int,
`name` varchar(30)
);

INSERT INTO类别(`id`,`name`)
VALUES(1,罐头食品),(2,'Ingredients'),(3''Meals');


解决方案

您使用sqlfiddle也可以轻松完成任务作为你试图用Select查询来解决问题。



它按照你想要的方式工作,我需要做的就是在类别表中添加一个左连接(IDK为什么你无法加入类别,因为它正常工作)。



所以。我编辑了你的select查询,如下所示:

  select Products.name,Filters。*,Categories.id from Products 
left join在Products.name上过滤
LIKE CONCAT('%',Filters.filter,'%')
左连接类别
on Categories.name = Filters.category_name
GROUP BY Products.name;

您将通过此查询得到您想要的结果。



现在,要使用此查询的结果更新 Products 表,您可以执行以下操作:

 更新产品
left join在Products.name上过滤
LIKE CONCAT('%',Filters.filter,'%')
左连接类别
on Categories.name = Filters.category_name
Set Products.category_name = Filters.category_name,
Products.category_id = Categories.id;

点击这里进行工作演示



希望它有帮助!


I have three tables (SQLFiddle with tables created)

Orange text is what I need to get by comparing Products.name with 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.

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:

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.

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).

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:

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!

这篇关于MySQL使用双左连接更新行,限制首次匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆