【问题标题】:I can´t use alias in sql delete我不能在 sql delete 中使用别名
【发布时间】:2013-07-31 22:33:58
【问题描述】:

我试过执行这个sql语句

delete 
        from reclamo r
        where exists ( select 1 from reclamo r
                        join cliente c on r.cod_cliente = c.cod_cliente
                        join localidad l on c.cod_localidad = l.cod_localidad
                        where l.descripcion = 'San Justo');

删除“圣胡斯托”镇客户提出的所有索赔 但它显示“错误代码:1064。您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解在 'r 附近使用的正确语法 哪里存在(从回收站中选择 1 在第 2 行加入 r.cod_cliente' 上的客户 c" 有谁知道我该如何解决这些错误?

这里的sqlfiddele:http://sqlfiddle.com/#!2/b2771

【问题讨论】:

  • 顺便说一句,您的 sqfiddle 不完整(可能是由于 ddl 字符限制)。

标签: mysql sql sql-delete


【解决方案1】:

如果您正在使用别名...您必须告诉实际删除什么(可能这是因为表别名通常仅在多表语法中需要...您可以省略别名):

mysql> delete from tablename r;
ERROR 1064 (42000): 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 'r' at line 1
mysql> delete r from tablename r;
Query OK, 0 rows affected (0.00 sec)

另见the manual:

注意
如果为表声明别名,则在引用表时必须使用别名: DELETE t1 FROM test AS t1, test2 WHERE ...

【讨论】:

  • 你不能使用 LIMIT
【解决方案2】:

不要使用别名。这是等效的逻辑,可以正常工作。

delete from reclamo
where cod_cliente in 
(select cod_cliente
from cliente c join localidad l on c.cod_localidad = l.cod_localidad
where l.descripcion = 'San Justo');

【讨论】:

    猜你喜欢
    • 2012-06-15
    • 2021-10-16
    • 2014-04-15
    • 2019-11-07
    • 2014-07-13
    • 2010-12-10
    • 2013-07-12
    • 2020-08-06
    • 1970-01-01
    相关资源
    最近更新 更多