数据库
首页 > 数据库> > MySQL选择最后一条记录并更新

MySQL选择最后一条记录并更新

作者:互联网

我想选择表中的最后一条记录并更新其名称.

UPDATE item 
SET name = (SELECT name FROM pds
            WHERE id = 9)
WHERE id=(SELECT id ORDER BY id DESC LIMIT 1);

但是,更改所有记录的执行名称时.

也尝试过:

UPDATE item 
SET name = (SELECT name FROM pds
            WHERE id = 9)
WHERE id=(SELECT id FROM item ORDER BY id DESC LIMIT 1);

解决方法:

在MySQL中,您可以将order by和limit子句应用于更新语句:

UPDATE item 
SET name = (SELECT name FROM pds
            WHERE id = 9)
ORDER BY id DESC
LIMIT 1

标签:sql-update,sql,mysql
来源: https://codeday.me/bug/20191118/2029818.html