我在 StackOverflow 中找到了一个适合我需求的解决方案,但是当我使用此解决方案时,仅更新第一个表:
这是我的查询:
UPDATE posts_flags
INNER JOIN
posts ON (posts_flags.content_id = posts.id )
SET
posts.published = 5,
posts_flags.status = 'new_state'
WHERE posts.id = ?;
仅更新 posts.published
,如果查询运行没有任何错误,posts_flags.status
也不会更改。
怎么了?谢谢
<小时/>更新
谢谢大家,真的很奇怪,现在它可以工作了,太奇怪了
最佳答案
查询看起来不错 - 这是证明
drop table if exists posts,posts_flags;
create table posts(id int,published int);
create table posts_flags(content_id int,status varchar(20));
insert into posts values(1,1);
insert into posts_flags values(1,null);
UPDATE posts_flags
INNER JOIN
posts ON (posts_flags.content_id = posts.id )
SET
posts.published = 5,
posts_flags.status = 'new_state'
WHERE posts.id = 1;
select *
from posts
join posts_flags on posts_flags.content_id = posts.id;
+------+-----------+------------+-----------+
| id | published | content_id | status |
+------+-----------+------------+-----------+
| 1 | 5 | 1 | new_state |
+------+-----------+------------+-----------+
1 row in set (0.00 sec)
如果您的模型不同,请将表定义和示例数据作为文本添加到问题中。
关于mysql - 使用单个查询更新两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58234968/