I can´t use alias in sql delete

只愿长相守 提交于 2019-12-30 10:49:33

问题


I tried execute this sql sentence

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');

for remove all the claims made by customers of the town of 'San justo' but it says "Error Code: 1064. 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 where exists ( select 1 from reclamo r join cliente c on r.cod_cliente' at line 2" anyone know how I can fix these errors?

sqlfiddele here: http://sqlfiddle.com/#!2/b2771


回答1:


If you're using aliases... ÿou have to tell what to actually delete (probably this is because table aliases are usually only needed in multiple table syntax... you could just omit the alias altogether):

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)

See als the manual:

Note
If you declare an alias for a table, you must use the alias when referring to the table: DELETE t1 FROM test AS t1, test2 WHERE ...




回答2:


Don't use an alias. This is equivalent logic and will work fine.

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');


来源:https://stackoverflow.com/questions/17982156/i-can%c2%b4t-use-alias-in-sql-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!