我希望在一个查询中为两个表更新具有相同值(99)的相同列(pid)。
UPDATE
product p,
inventory i
SET
p.pid = 99,
i.pid = 99
WHERE
i.ItemName = 'ES1'
给了我一个错误:
1032 - Can't find record in 'inventory'
虽然它在那里,我可以用:
获取它SELECT *
FROM inventory i
WHERE
i.ItemName = 'ES1'
答案 0 :(得分:0)
我无法重现你的问题。如果您的模型看起来像这样
drop table if exists p,i;
create table p (id int,pid int);
create table i (id int, pid int, itemname varchar(3));
insert into p values(1,null),(2,null);
insert into i values(1,null,'ES1'),(2,null,'abc');
then
UPDATE
p,
i
SET
p.pid = 99,
i.pid = 99
WHERE
i.ItemName = 'ES1'
;
结果如预期
MariaDB [sandbox]> select * from p;
+------+------+
| id | pid |
+------+------+
| 1 | 99 |
| 2 | 99 |
+------+------+
2 rows in set (0.00 sec)
MariaDB [sandbox]> select * from i;
+------+------+----------+
| id | pid | itemname |
+------+------+----------+
| 1 | 99 | ES1 |
| 2 | NULL | abc |
+------+------+----------+
2 rows in set (0.00 sec)
如果您将示例数据作为文字添加到问题中,可能会有所帮助。