删除MySQL上的重复行(至少留下一个)

发布于 2024-12-20 01:10:30 字数 406 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人九事 2024-12-27 01:10:30

创建新表更简单。以前的答案很好,但我喜欢这样:
创建一个具有“lang_original、lang_target、word”唯一键的新表

CREATE TABLE new_table_can_be_renamed_later ( 
  ..your-fields...
  UNIQUE unique (lang_original,lang_target,workd)
);

,然后通过选择旧表来填充新表,并在 INSERT 中使用 IGNORE

INSERT IGNORE INTO new_table_can_be_renamed_later 
  SELECT * FROM original_table

请考虑 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"

CREATE TABLE new_table_can_be_renamed_later ( 
  ..your-fields...
  UNIQUE unique (lang_original,lang_target,workd)
);

Then fill your new table with by selecting the old table and use IGNORE in your INSERT

INSERT IGNORE INTO new_table_can_be_renamed_later 
  SELECT * FROM original_table

Please consider Mysql docs for right syntax.

二智少女猫性小仙女 2024-12-27 01:10:30

可以像这样工作:

DELETE FROM tbl
WHERE  EXISTS (
   SELECT *
   FROM   tbl t
   WHERE (t.lang_original,   t.lang_target,   t.word)
       = (tbl.lang_original, tbl.lang_target, tbl.word)
   AND tbl.id_word > t.id_word
   )

如果@Jason是对的,并且MySQL不允许引用删除表,那么这是另一种独立工作的形式:

DELETE FROM tbl
USING (
   SELECT min(id_word) AS min_id, lang_original, lang_target, word
   FROM   tbl t
   GROUP  BY lang_original, lang_target, word
   HAVING count(*) > 1
   ) x
WHERE (tbl.lang_original, tbl.lang_target, tbl.word)
   =  (  x.lang_original,   x.lang_target,   x.word)
AND    tbl.id_word > x.min_id

两种变体都保留具有最小id的重复项并杀死其余的。

如果您想首先将所有翻译保存到一组重复项中具有最小 id 的单词:

UPDATE tbl SET translation = all_trans
FROM  (
   SELECT min(id_word) AS min_id, group_concat(translation) AS all_trans
   FROM   tbl
   GROUP  BY lang_original, lang_target, word
   HAVING count(*) > 1
   ) t
WHERE  tbl.id_word = t.min_id

Could work like this:

DELETE FROM tbl
WHERE  EXISTS (
   SELECT *
   FROM   tbl t
   WHERE (t.lang_original,   t.lang_target,   t.word)
       = (tbl.lang_original, tbl.lang_target, tbl.word)
   AND tbl.id_word > t.id_word
   )

If @Jason is right, and MySQL does not allow to reference the delete table, here is another form that works independently:

DELETE FROM tbl
USING (
   SELECT min(id_word) AS min_id, lang_original, lang_target, word
   FROM   tbl t
   GROUP  BY lang_original, lang_target, word
   HAVING count(*) > 1
   ) x
WHERE (tbl.lang_original, tbl.lang_target, tbl.word)
   =  (  x.lang_original,   x.lang_target,   x.word)
AND    tbl.id_word > x.min_id

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:

UPDATE tbl SET translation = all_trans
FROM  (
   SELECT min(id_word) AS min_id, group_concat(translation) AS all_trans
   FROM   tbl
   GROUP  BY lang_original, lang_target, word
   HAVING count(*) > 1
   ) t
WHERE  tbl.id_word = t.min_id
赏烟花じ飞满天 2024-12-27 01:10:30

我不确定你能做到吗?
你最好做一些类似的事情,

select distinct * into yournewtable from originaltable

这可能会有效。

I'm not sure that you can do that.
You are probably better off doing something like

select distinct * into yournewtable from originaltable

That may work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文