Mysql DELETE 不能使用别名? 是我不会用!

        今天碰到一个sql问题,就是在delete中加了别名,导致报错了:"[Err] 1064 - You have an error in your SQL syntax; ..."

        简单说下过程,本来是一个简单的delete语句:

delete from table1 where status=2;

        后需要增加关联条件,所以在后边追加了where条件,为了关联写着方便为表添加了别名,变为:

delete from table1 a where a.status=2
and EXISTS (select b.id from table2 b where b.fid=a.id)

        就是这样报错了,提示语法错误,觉得挺纳闷,将delete 换成 select * (通常为了省事,验证sql的时候换成select),没有问题,执行正确。

        百度很多说“mysql delete 不能使用别名”的,去掉别名后果然正常:

delete from table1 where status=2
and EXISTS (select id from table2 where table2.fid=table1.id)

        虽然问题解决了,但是更纳闷了,delete不能用别名?!,当然不是,是我用错了!

delete a from table1 a where a.status=2
and EXISTS (select b.id from table2 b where b.fid=a.id)

        如上,delete使用别名的时候,要在delete和from间加上删除表的别名,这样才是正确的写法。

        DELETE的语法详见(这是MySQL的要求,在oracle上没有)


猜你喜欢

转载自blog.csdn.net/mingjia1987/article/details/79741113