[英]How to select the last row from one table then update the last row of another one?
I'm doing this 我在做这个
UPDATE table1
SET table1.col1 = table2.col2
FROM table2 WHERE table1.id = (SELECT MAX(id) FROM table1) AND table2.ID = (SELECT MAX(ID) FROM table2);
I have a syntax error, but I don't know how to do it. 我有语法错误,但我不知道该怎么做。
Assuming that id
is unique in table1: 假设
id
在table1中是唯一的:
UPDATE table1 t1
SET t1.col1 = (select t2.col2 from table2 t2 order by id desc limit 1)
ORDER BY t1.id DESC
LIMIT 1;
This updates the "last" row in table1
(by id
) with the "last" row in table2
(by id
). 此更新“最后”行
table1
(通过id
用在“最后”行) table2
(按id
)。
Your syntax doesn't work in multiple ways: 您的语法无法以多种方式工作:
FROM
clause in UPDATE
. UPDATE
的FROM
子句。 MySQL 的MySQL
update table1
cross join (
select col2
from table2
order by id desc
limit 1) as A
inner join (
select id
from table1
order by id desc
limit 1) as B on (B.id = table1.id)
set table1.col1 = A.col2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.