另一个表中的mysql-update表

时间:2022-09-07 17:05:01

How can i update a table if a value from another table. a illustrate is like this

如果来自另一个表的值,我如何更新表。说明是这样的

table products

product_name | cat_name | id_cat
Item 1       |  sport   |   
Item 2       |  food    |
Item 3       |  fashion |

table category

id_cat | cat_name  
 1     | sport
 2     | food
 3     | fashion 

please give me a query or function how to update field id_cat from table product which value from table category?

请给我一个查询或函数如何更新表产品中的字段id_cat哪个值来自表类别?

thanks

1 个解决方案

#1


1  

you can simply join both tables,

你可以简单地加入两个表,

UPDATE  products a
        INNER JOIN category b
            ON a.cat_name = b.cat_name
SET     a.id_cat = b.id_cat

for faster performance, add an index on column cat_name for both tables.

为了提高性能,请在两个表的列cat_name上添加索引。

#1


1  

you can simply join both tables,

你可以简单地加入两个表,

UPDATE  products a
        INNER JOIN category b
            ON a.cat_name = b.cat_name
SET     a.id_cat = b.id_cat

for faster performance, add an index on column cat_name for both tables.

为了提高性能,请在两个表的列cat_name上添加索引。