MySQL根据选择结果更新表

时间:2019-03-12 15:43:19

标签: mysql

我有两个桌子

__________________________________
|      products_description       |
__________________________________
 products_id | products_description
----------------------------------

__________________________________
|     products_to_categories     |
__________________________________
  products_id  | id_categories     
----------------------------------

我需要更新ID为'309'的产品的“ products_description”。 所以我想我需要做一个

SELECT to products_to_categories WHERE id_categories = 309 

并根据这些结果得出

UPDATE to "products_description" to SET "my string" WHERE 
products_description.products_id = products_to_categories.products_id.

我到处都有搜索,但是找不到所需的示例,显然我对MySQL不够好

2 个答案:

答案 0 :(得分:4)

UPDATE products_description 
SET products_description = 'YOUR TEXT' 
WHERE product_id IN (
  SELECT products_id FROM products_to_categories WHRE id_categories = 309
)

顺便说一句,为您的表和列尝试更好的名称。如果您的表名为products_descriptions,则无需命名其列之一products_description,只需将其命名为description

答案 1 :(得分:0)

我认为您必须编写一个子查询。 例如。

UPDATE products_description
SET products_description = (SELECT your_column FROM products_to_categories WHERE id_categories = products_description.products_id LIMIT 1)