MySQL 删除表中重复的记录(where......)
2008-12-04 13:44
274 查看
delete from at1 where id in (select * from (select max(id) from at1 group by str having count(str) > 1) as b); or delete a from at1 as a, at1 as b where a.str=b.str and a.id > b.id; 3. 多个字段重复的问题。 “name”值存在重复的项; Select Name,Count(*) From A Group By Name Having Count(*) > 1 如果还查性别也相同大则如下: Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1 (见http://hi.baidu.com/yangjifu/blog/item/723e5f4cf92708ffd72afc92.html) ----------------------------------------- 1.MySQL的delete 支持: DELETE t1,t2 FROM t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id or DELETE FROM t1,t2 USING t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id 2. 从 MySQL 4.0.4 开始,你也可以执行一个包含多个表的 UPDATE 的操作: UPDATE items,month SET items.price=month.price WHERE items.id=month.id; 注意:多表 UPDATE 不可以使用 ORDER BY 或 LIMIT。 3. 查询一个表中重复的记录: select max(id) from at1 group by str having count(str) > 1 select a.id from at1 as a, at1 as b where a.str=b.str and a.id > b.id 4. delete from at1 where id in (select max(id) from at1 group by str having count(str) > 1); 是错误的: ERROR 1093 : You can't specify target table 'at1' for update in FROM clause 正确写法:delete from at1 where id in (select * from (select max(id) from at1 group by str having count(str) > 1) as b); 理由(文档): 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: Exception: The preceding prohibition does not apply if you are using a subquery for the modified table in the FROMclause. Example: Here the prohibition does not apply because a subquery in the FROMclause is materialized as a temporary table, so the relevant rows in thave already been selected by the time the update to ttakes place. mysql还支持 where (a,b) in (select a, b from aaaa) |
相关文章推荐
- MySQL 删除表中重复的记录(where......)
- mysql 删除重复记录保存一条
- MySql-如何查询删除数据表重复记录
- mysql删除同一表中重复字段记录
- 解决Mysql中删除重复记录的问题
- mysql给列的属性赋值,修改列属性,删除记录,删除所有记录,查询表的记录,去除重复的,总和查询,条件查询,模糊查询,关联查询,范围查询
- mysql,oracle里删除重复记录,只保留一条(除了自增长字段,其他的字段完全相同为相同记录)
- mysql删除重复记录,保存Id最小的一条
- Mysql 删除重复记录,只保留最小的一条
- mysql删除重复记录语句的方法
- MySql中删除重复垃圾记录
- MySQL之——查询重复记录、删除重复记录方法大全
- mysql删除重复记录
- mysql删除重复记录,保留id最少
- MySQL查询重复字段,及删除重复记录的方法
- 删除数据库中多余的重复记录(在mysql的数据库上测试过,成功删除)
- 关于mysql中删除重复记录,并保留重复数据中的一条数据的SQL语句理解
- mysql怎么查询、删除重复记录?
- MySQL删除表中多余的重复记录,只保留一条记录
- MySql删除重复记录问题、嵌套删除(转)