MYSQL 查询给出语法错误
MYSQL query gives syntax error
我是 运行 这个关于 phpmyadmin 的查询,它给出了一个语法错误,
Delete from users_roles as ur where ur.uid NOT IN (select u.uid from users as u)
ERROR: 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 'as ur where ur.uid NOT IN (select u.uid from users as u)' at line 1
当我运行这个查询
select ur.uid from users_roles as ur where ur.uid NOT IN (select u.uid from users as u)
它给了我结果。
尝试删除别名:
Delete from users_roles where uid NOT IN (select uid from users)
请参阅 SQL Fiddle 中的演示。
编辑:
当您需要使用别名进行删除查询时(当您使用联合查询时),您可以这样做:
Delete ur from users_roles as ur where ur.uid NOT IN (select u.uid from users as u)
您不能在 DELETE
语句的那种形式中使用别名。应该是:
DELETE FROM users_roles
WHERE uid NOT IN (SELECT uid FROM users)
要使用别名,您必须使用不同的语法:
DELETE ur FROM users_roles AS ur
WHERE ur.uid NOT IN (SELECT uid FROM users)
此语法通常仅在您在 DELETE
语句中执行 JOIN
时使用,因此您需要在 table 中引用特定的 table 和列=14=]。当您只引用一个 table 时,不需要它。例如,您可以将查询重写为:
DELETE ur
FROM users_roles AS ur
LEFT JOIN users AS u ON ur.uid = u.uid
WHERE u.uid IS NULL
参见MySQLdocumentation。 DELETE
第一种形式只允许tbl_name,第二种形式允许table_references,后者是可以定义别名的地方。
不要使用别名删除试试这个
从 users_roles 中删除,其中 users_roles.uid NOT IN (select u.uid from users as u)
我是 运行 这个关于 phpmyadmin 的查询,它给出了一个语法错误,
Delete from users_roles as ur where ur.uid NOT IN (select u.uid from users as u)
ERROR: 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 'as ur where ur.uid NOT IN (select u.uid from users as u)' at line 1
当我运行这个查询
select ur.uid from users_roles as ur where ur.uid NOT IN (select u.uid from users as u)
它给了我结果。
尝试删除别名:
Delete from users_roles where uid NOT IN (select uid from users)
请参阅 SQL Fiddle 中的演示。
编辑:
当您需要使用别名进行删除查询时(当您使用联合查询时),您可以这样做:
Delete ur from users_roles as ur where ur.uid NOT IN (select u.uid from users as u)
您不能在 DELETE
语句的那种形式中使用别名。应该是:
DELETE FROM users_roles
WHERE uid NOT IN (SELECT uid FROM users)
要使用别名,您必须使用不同的语法:
DELETE ur FROM users_roles AS ur
WHERE ur.uid NOT IN (SELECT uid FROM users)
此语法通常仅在您在 DELETE
语句中执行 JOIN
时使用,因此您需要在 table 中引用特定的 table 和列=14=]。当您只引用一个 table 时,不需要它。例如,您可以将查询重写为:
DELETE ur
FROM users_roles AS ur
LEFT JOIN users AS u ON ur.uid = u.uid
WHERE u.uid IS NULL
参见MySQLdocumentation。 DELETE
第一种形式只允许tbl_name,第二种形式允许table_references,后者是可以定义别名的地方。
不要使用别名删除试试这个 从 users_roles 中删除,其中 users_roles.uid NOT IN (select u.uid from users as u)