dongpo5264 2010-04-26 22:23
浏览 99

如何在MySQL中查找重复的字段值并将其删除

Consider we have a field named (username) in our table named (tpl_users), now this table has lots of duplicated rows

I wrote this code to delete duplicated usernames:

Delete FROM tpl_users WHERE username = username;

How is it possible to delete duplicated usernames?

  • 写回答

4条回答 默认 最新

  • donglang1976 2010-04-26 22:37
    关注

    Your query deletes all the rows where the user name is not NULL.

    If you want to identify the user names which are associated with more than one row:

    SELECT username
      FROM tpl_users
     GROUP BY username
    HAVING COUNT(*) > 1;
    

    Before converting that into a DELETE, you need to be aware that the majority of queries will delete all the records associated with a duplicated name, rather than retaining just one of the duplicates.

    评论

报告相同问题?

悬赏问题

  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏