MySQL更新列基于部分查询结果

时间:2017-03-20 20:33:40

标签: mysql

我正在尝试根据MySQL查询获取更新一个表列。 问题是两列中的查询结果,但我只需要更新类别列。

not_categorized

--------------------------------------------------
| id | category_lookup      | category1          |
--------------------------------------------------
| 1  | Chino                | *to be found*      |
--------------------------------------------------
| 2  | Rainjacket           | *to be found*      |
--------------------------------------------------

title_match

--------------------------------------------------
| id | title                | category1          |
--------------------------------------------------
| 1  | Chino                | Trousers / Jeans   |
--------------------------------------------------
| 2  | Rainjacket           | Jackets / Coats    |
--------------------------------------------------

查询

SELECT distinct not_categorized.id,  category_lookup
FROM not_categorized
left JOIN title_match ON  not_categorized.category_lookup LIKE CONCAT('%', title_match.title, '%')
group by id; 

这给了我正确的结果。基于" Chino"该类别应该是"牛仔裤/裤子"。

查询结果

-------------------------
| id | category         | 
-------------------------
| 1  | Trousers / Jeans |
-------------------------
| 2  | Jackets / Coats  |
-------------------------

我现在如何更新列" category1"在" not_characterized"表?

这不起作用:

update not_categorized
set category1 = **QUERY**

1 个答案:

答案 0 :(得分:0)

查看您的数据,您可以使用基于title和category_lookup

的内部联接
  update not_categorized
  inner join title_match on not_categorized.category_lookup =  title_match.title
  set not_categorized.category1 = title_match.category1