首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >MySQL 中 delete 语句的子查询限制

MySQL 中 delete 语句的子查询限制

作者头像
用户1212940
发布2022-04-13 15:28:45
发布2022-04-13 15:28:45
5.5K0
举报
文章被收录于专栏:LambdaLambda

场景一

代码语言:javascript
复制
delete from student where id = (select max(id) from student);

[Err] 1093 - You can't specify target table 'student' for update in FROM clause
描述: 如果子查询的 from 子句和更新、删除对象使用同一张表,会出现上述错误。

解决方法: 通过给 from 子句中的结果集起别名。

代码语言:javascript
复制
delete from student where id = (select n.max_id from (select max(id) as max_id from student) as n);

上述情况对于 in 子句也适用

代码语言:javascript
复制
delete from student where id in (select id from student where id > 30);
[Err] 1093 - You can't specify target table 'student' for update in FROM clause
解决方法同上:
代码语言:javascript
复制
delete from student where id in (select n.id from (select id from student where id > 30) as n);

场景二

代码语言:javascript
复制
delete from student m where m.id = 1;

[Err] 1064 - You have an error in your SQL syntax;
描述: delete from table 这样的句子中 table 不能使用别名。

解决方法:去掉别名:

代码语言:javascript
复制
delete from student where id = 1;
Whatever is worth doing is worth doing well.
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/09/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档