17,377
社区成员
发帖
与我相关
我的任务
分享
delete from student2 where id not in(select min(id) from student2 group by name);
delete from student2 where id not in(select mid from (select min(id) mid
from student2 group by name) as t);
mysql对子查询的支持是比较薄弱的 ,就是说 update的where语句中不能子查询
在MySQL手册中的错误代码:
错误:1093 SQLSTATE: HY000 (ER_UPDATE_TABLE_USED)
消息:不能在FROM子句中制定要更新的目标表'%s'。
Mysql手册中的相关描述:
一般而言,不能更改表,并从子查询内的相同表进行选择。
例如,该限制适用于具有下述形式的语句:
DELETE FROM t WHERE ... (SELECT ... FROM t ...);
UPDATE t ... WHERE col = (SELECT ... FROM t ...);
{INSERT|REPLACE} INTO t (SELECT ... FROM t ...);
例外:如果为FROM子句中更改的表使用子查询,前述禁令将不再适用。
例如:
UPDATE t ... WHERE col = (SELECT (SELECT ... FROM t...)
AS _t ...);
禁令在此不适用,这是因为FROM中的子查询已被具体化为临时表,因此 “t”中的相关行已在满足“t”条件的情况下、在更新时被选中。
English:
· In general, you cannot modify a table and select from the same table in a subquery. For example, this limitation applies to statements of the following forms:
DELETE FROM t WHERE ... (SELECT ... FROM t ...);
UPDATE t ... WHERE col = (SELECT ... FROM t ...);
{INSERT|REPLACE} INTO t (SELECT ... FROM t ...);
Exception: The preceding prohibition does not apply if you are using a subquery for the modified table in the FROM clause. Example:
UPDATE t ... WHERE col = (SELECT (SELECT ... FROM t...) AS _t ...);
Here the prohibition does not apply because a subquery in the FROM clause is materialized as a temporary table, so the relevant rows in t have a
解决:
依据手册将该命令改为:
UPDATE forum_members AS A
INNER JOIN (
SELECT M_NAME
FROM forum_members
GROUP BY M_NAME
HAVING count( * ) >1
) AS B ON A.M_NAME = B.M_NAME
SET A.is_multi =1