我在尝试MySQL(5.1.41-3ubuntu12.10-log)使用数据库一次性查询while循环: ; Y& w) M4 {" i7 S2 ZWHILE ((SELECT COUNT(*) FROM (SELECT id,COUNT(*) AS cnt FROM foo GROUP BY id ORDER BY COUNT(*) DESC) cnts WHERE cnt > 1) != 0) DOBEGINSET @curr_id = (SELECT id FROM (SELECT id,COUNT(*) AS cnt FROM foo GROUP BY id ORDER BY COUNT(*) DESC) cnts WHERE cnt > 1 LIMIT 1);SET @new_id = (SELECT MAX(id) FROM foo);UPDATE foo SET id = @new_id WHERE id = @curr_id LIMIT 1;END WHILE;这是 有多个是一样的ID使用下一个记录ID其中一个更新。* T' O5 A6 U7 M! n) Z
主体中的语法是正确的,while语句中使用的谓词也可以在没有任何麻烦的情况下独立执行。MySQL在查询开始时返回语法错误: 7 i1 Z/ F+ o* n4 q9 OError Code : 1064You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE ((SELECT count(*) FROM (SELECT id,COUNT(*) AS cnt FROM stock_produ' at line 1我意识到这可能不是 正确 的做事方式,但这是一个非常糟糕的数据库(或者根本不是全部),所以这样工作会很棒。 . q- N) `2 Q9 e4 f4 K. d4 R) t谢谢, 6 a4 M! c) [, S/ j' H罗宾3 G2 v3 Z6 {' i8 ^