mysql DELETE FROM `table` AS `alias` ... WHERE `alias`.`column` ...为什么会出现语法错误?

v09wglhw  于 2023-06-04  发布在  Mysql
关注(0)|答案(4)|浏览(634)

我在MySQL中试过:

DELETE FROM `contact_hostcommands_relation` AS `ContactHostCommand` WHERE (`ContactHostCommand`.`chr_id` = 999999) LIMIT 1

我得到了这个:

#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 'WHERE (`ContactHostCommand`.`chr_id` = 999999) LIMIT 1' at line 1
  • 注意:* 此查询是自动生成的,条件基于表别名。

为什么会出现这个错误?
在where子句中是否有使用表别名的方法?
这是MySQL特有的吗?

ovfsdjhp

ovfsdjhp1#

@Matus和@CeesTimmerman关于MSSQL的说法也适用于MySQL 5.1.73:

delete <alias> from <table> <alias> where <alias>.<field>...
ymzxtsji

ymzxtsji2#

你可以这样使用SQL:

DELETE FROM ContactHostCommand 
USING `contact_hostcommands_relation` AS ContactHostCommand 
WHERE (ContactHostCommand.`chr_id` = 999999) 
LIMIT 1
3zwjbxry

3zwjbxry3#

您不能在MySQL的DELETE子句中使用AS

DELETE FROM `contact_hostcommands_relation` WHERE (`chr_id` = 999999) LIMIT 1
k2arahey

k2arahey4#

如果您想在MySQL上使用Alias,请尝试使用:

DELETE <alias_name> FROM <alias_table> <alias_name> WHERE <alias_name>.nameofcolumn LIKE '%XXXXX%'

相关问题