MySQL语法相关问题一

-纯碎个人记录,非喜勿喷

MySQL DELETE语句起别名报错问题

问题一

刷Leetcode的时侯写了这样一个SQL:

DELETE from person p0 where p0.id in(

SELECT p1.id from person p1,person p2
where 
p1.email = p2.email
and p1.id > p2.id
)
Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'p0 where p0.id in(

SELECT p1.id from person p1,person p2
where 
p1.email = p2.e' at line 1

解决:

mySql 的Delete语句在使用别名时,与其他语句语法有些许不一致,除了在表名后边加上别名外,还需要在Delete关键字后边也加上别名。注意:两处别名需要一致

新SQL

DELETE p from person p where p.id in(

SELECT p1.id from person p1,person p2
where 
p1.email = p2.email
and p1.id > p2.id

)

问题二

报错

Error : You can't specify target table 'p' for update in FROM clause

You can’t specify target table ‘表名’ for update in FROM clause

翻译为:不能先select出同一表中的某些值,再update这个表(在同一语句中)

正确的写法

DELETE p from person p where p.id in(

select a.id from (
SELECT p1.id from person p1,person p2
where 
p1.email = p2.email
and p1.id > p2.id
)as a

)

格式

也就是说:把结果集当作一个表,自我查询一遍

格式为:SELECT a.StudentResult FROM

            (结果集)a

删除成功

在这里插入图片描述

参考

MySQL Delete语句不能用别名问题
https://blog.csdn.net/hy_coming/article/details/113122781

Mysql DELETE 不能使用别名? 是我不会用!
https://blog.csdn.net/mingjia1987/article/details/79741113

mysql出现“ You can’t specify target table ‘表名’ for update in FROM clause”解决方法
https://blog.csdn.net/PoetsSociety/article/details/82391523

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值