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


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

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

    delete from table1 where status=2

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

            就是这样报错了,提示语法错误,觉得挺纳闷,将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上没有)

  • 相关阅读:
    613. Shortest Distance in a Line
    182. Duplicate Emails
    181. Employees Earning More Than Their Managers
    180. Consecutive Numbers
    612. Shortest Distance in a Plane
    178. Rank Scores
    177. Nth Highest Salary
    610. Triangle Judgement
    二维气泡弹窗
    jQuery触发a标签的click事件和原生javascript的区别
  • 原文地址:https://www.cnblogs.com/zhurong/p/9474465.html
Copyright © 2020-2023  润新知