产品表中有很多产品。每个产品都有product_specification。我需要将所有product_specification.document_type_id更新为' 2'的值。这是我在阅读SO时想出的:
update product_specifications
set product_specifications.document_type_id = 2
inner join products
on products.product_specification_id = product_specifications.id
where products.id in (
select p.id from products as p
inner join product_specifications as ps
on p.product_specification_id = ps.id
where p.store_front_id = 71
and ps.document_type_id = 1)
但这不起作用......
答案 0 :(得分:1)
我没有看到使用两个查询带来的好处,因为您要加入相同的表格。您可以将JOIN
与UPDATE
一起使用(请参阅回答here)< / p>
UPDATE product_specifications ps INNER JOIN products p
ON ps.id=p.product_specification_id
SET ps.document_type_id = 2
WHERE p.store_front_id = 71 AND ps.document_type_id = 1
编辑:更多信息
如您所知,单表和多表的UPDATE语法(docs here)如下:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET ...`
并且如文档
中所述的几行所述对于多表语法,UPDATE更新每个名为的表中的行 在满足条件的 table_references 中。
以下是有关table_references
的文档答案 1 :(得分:0)
我认为你的查询有点不正确,我想你应该获得语法错误,因为你应该遵循语法:
update tableName join anotherTableName set ... where ...
我觉得你的查询应该是这样的:
update product_specifications
inner join products
on products.product_specification_id = product_specifications.id
set product_specifications.document_type_id = 2
where products.id in (
select p.id from products as p
inner join product_specifications as ps
on p.product_specification_id = ps.id
where p.store_front_id = 71 and ps.document_type_id = 1
)
我没有检查这个查询,但我写了很多次相同的查询,我真的相信它会帮助你......