gpt4 book ai didi

mysql - 根据条件删除 SQL 数据库中的重复项

转载 作者:可可西里 更新时间:2023-11-01 08:15:23 26 4
gpt4 key购买 nike

我需要从如下所示的表中删除重复项:

id           post_author    post_title
-----------------------------------------------------------------------------
21319 1 Youngstown State University
20535 1 Yo San University of Traditional Chinese Medicine
30268 29 Yo San University of Traditional Chinese Medicine
29747 29 Yeshiva University
21964 1 Yale University
29247 29 Yale University
29497 29 Xavier University of Louisiana
21916 1 Xavier University
29862 29 Xavier University
29860 29 Wright State University-Main Campus
20915 1 Wright State University-Lake Campus
21562 1 World Mission University
30267 29 World Mission University

基本上,如果有两个条目具有相同的 post_title,我需要删除带有 post_author = 1 的条目,但是如果 post_title 是唯一的,那么条目应该保持原样。

如何使用 SQL 查询完成此操作?

编辑:

我尝试了 Mureinik 建议的查询。查询看起来像这样:

DELETE t FROM wp_posts AS t 
WHERE t.post_author = 1 AND
EXISTS (SELECT * FROM wp_posts s
WHERE t.post_title = s.post_title
AND s.post_authot != 1)

但是我得到了错误:

[Err] 1093 - You can't specify target table 't' for update in FROM clause

我做错了什么?

最佳答案

您可以使用 exists 运算符:

DELETE FROM my_table t
WHERE post_author = 1 AND
EXISTS (SELECT *
FROM my_table s
WHERE t.post_title = s.post_title AND
s.post_author != 1)

关于mysql - 根据条件删除 SQL 数据库中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30256706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com