删除MySQL上的重复行(至少留下一个)
我在 MySQL 中有这个表:
- id_word
- lang_original (原始单词的语言) VARCHAR(2)
- lang_target (翻译单词的语言) VARCHAR(2)
- word (单词本身)VARCHAR(50)
- 翻译(翻译)VARCHAR(50)
它们不应该有重复项。是否有可能有一个 sql 查询来查找重复项并将其删除(保留第一个匹配项未删除)?
更新重复项将具有相同的 lang_original、lang_target 和单词(仅这 3 个字段)。
I have this table in MySQL:
- id_word
- lang_original (the language from the original word) VARCHAR(2)
- lang_target (the language from the translated word) VARCHAR(2)
- word (the word itself) VARCHAR(50)
- translation (the translation) VARCHAR(50)
They should not have duplicates. Is it possible to have a sql query that finds duplicates and deletes them (leaving the first match undeleted)?
Update a duplicate would be something that has the same lang_original,lang_target and word (only those 3 fields).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建新表更简单。以前的答案很好,但我喜欢这样:
创建一个具有“lang_original、lang_target、word”唯一键的新表
,然后通过选择旧表来填充新表,并在 INSERT 中使用 IGNORE
请考虑 Mysql 文档以获得正确的语法。
It's simpler to create a new table. Previous answers are good, but I like it this way:
Create a new table with a unique key for "lang_original, lang_target, word"
Then fill your new table with by selecting the old table and use IGNORE in your INSERT
Please consider Mysql docs for right syntax.
可以像这样工作:
如果@Jason是对的,并且MySQL不允许引用删除表,那么这是另一种独立工作的形式:
两种变体都保留具有最小id的重复项并杀死其余的。
如果您想首先将所有翻译保存到一组重复项中具有最小 id 的单词:
Could work like this:
If @Jason is right, and MySQL does not allow to reference the delete table, here is another form that works independently:
Both variants leave the duplicate with the smallest id alive and kill the rest.
If you want to save all your translations to the word with the smallest id in a group of dupes first:
我不确定你能做到吗?
你最好做一些类似的事情,
这可能会有效。
I'm not sure that you can do that.
You are probably better off doing something like
That may work.