我正在做这个
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);
我有语法错误,但我不知道该怎么做。
答案 0 :(得分:1)
假设{1}在table1中是唯一的:
id
这会更新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;
中的“最后”行(table1
)和id
中的“最后”行(table2
)。
您的语法不能以多种方式工作:
id
中的FROM
子句。答案 1 :(得分:0)
的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