MySQL怎么去掉表中重复记录_MySQL 删除表中重复的记录,只保留一条

2.

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

-----------------------------------------

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: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 theFROMclause. Example:UPDATE t ... WHERE col = (SELECT (SELECT ... FROM t...) AS _t ...);

Here the prohibition does not apply because a subquery in theFROMclause is materialized as a temporary table, so the relevant rows inthave already been selected by the time the update tottakes place.

mysql还支持 where (a,b) in (select a, b from aaaa)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值