mysql更新语法似乎不起作用。 更新语法上的子查询是否有限制?
不起作用:
update books set imagename ='name'
where book_ID='(select book_ID from books order by book_ID desc limit 1)';
工作:
update books set imagename ='name'
where book_ID='101';
答案 0 :(得分:0)
MySQL使用正在更新的表确实对子查询有限制。所以不允许你这样做:
update books
set imagename ='name'
where book_ID = (select book_ID from books order by book_ID desc limit 1);
相反,您可以这样做:
update books
set imagename ='name'
order by book_ID desc
limit 1;
以下是documentation中的解释:
通常,您无法修改表并从同一个表中进行选择 在子查询中。例如,此限制适用于 以下形式:
DELETE FROM t WHERE ... (SELECT ... FROM t ...); UPDATE t ... WHERE col = (SELECT ... FROM t ...); {INSERT|REPLACE} INTO t (SELECT ... FROM t ...);
异常:如果在FROM子句中使用子查询来修改表,则前面的禁止不适用。例如:
UPDATE t ... WHERE col = (SELECT * FROM (SELECT ... FROM t...) AS _t ...); Here the result from the subquery in the FROM clause is stored as a temporary table, so the relevant rows in t have already been
在更新到t时选择。
答案 1 :(得分:0)
尝试此查询。
Update books
Set imagename = name"
Where book_ID In (
Select T1.book_ID
From books As T1
Where T1.book_ID = '101'
)