4

我试过执行这个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');

删除“San justo”镇的客户提出的所有索赔,但它显示“错误代码:1064。您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册以获取正确的语法使用'r where exists (select 1 from reclamo r join cliente c on r.cod_cliente' at line 2" 附近'r where exists" 有人知道我该如何解决这些错误吗?

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

4

2 回答 2

4

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

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)

参见手册

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

于 2013-07-31T22:38:16.473 回答
1

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

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');
于 2013-07-31T22:42:35.050 回答