MySQL 中删除按除 ID 之外的所有分组的重复行的最佳方法?

发布于 2024-12-11 11:54:43 字数 499 浏览 0 评论 0原文

似乎不是重复的,因为这个问题是不同的。

我的表有 3 列:id、col2、col3。我正在使用这种方法来删除重复的行:

create table temp_table as
select * from my_table where 1 group by col2,col3;

drop table my_table;    
rename table temp_table to my_table;

然而,my_table 实际上有很多列,而不仅仅是 3 列,在查询中列出来很痛苦。所以我想问是否有一种方法可以进行这样的查询:

create table temp_table as
select * from my_table where 1 group by * except id;

drop table my_table;    
rename table temp_table to my_table;

有可能的方法吗?

Doesn't seem to be a duplicate coz this question is different.

My table has 3 columns: id,col2,col3. I'm using this method to remove duplicated rows:

create table temp_table as
select * from my_table where 1 group by col2,col3;

drop table my_table;    
rename table temp_table to my_table;

However, my_table actually has a lot of columns, not just 3, painfully to list out in the query. So I want to ask whether there's a way that we can do a query something like this:

create table temp_table as
select * from my_table where 1 group by * except id;

drop table my_table;    
rename table temp_table to my_table;

Is there a possible way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

撩动你心 2024-12-18 11:54:43

您可以执行子查询以确保您获得的内容是唯一的。此查询将为您提供重复项(保留具有较低 ID 的重复项):

SELECT id
  FROM duplicates d1
 WHERE EXISTS (
    SELECT 1
      FROM duplicates d2
     WHERE d2.col2 = d1.col2
       AND d2.col3 = d1.col3
       AND d2.id < d1.id
)

将它们放入临时表中(或将它们加载到 PHP)并运行第二个查询来删除。 (在读取数据时无法修改表)

执行 WHERE NOT EXISTS 来获取要保留的元素的 ID(同样,保留具有最低 ID 的元素)。

You could do a sub-query to make sure that what you get is unique. This query will give you the duplicates (preserving the ones with the lower IDs):

SELECT id
  FROM duplicates d1
 WHERE EXISTS (
    SELECT 1
      FROM duplicates d2
     WHERE d2.col2 = d1.col2
       AND d2.col3 = d1.col3
       AND d2.id < d1.id
)

throw them into a temporary table (or load them to PHP) and run a second query to DELETE. (You can't modify a table while you're reading from it)

Do a WHERE NOT EXISTS to get the IDs of the elements to preserve (again, the ones with the lowest IDs are kept).

白云悠悠 2024-12-18 11:54:43

我找到了一种方法“删除除 ID 之外的所有数据分组的重复行”,但它不纯粹是 MySQL,需要额外的 PHP 代码,而且太长了:

$mysqli = mysqli_connect(...);

function remove_duplicated_rows($table_name) {
  global $mysqli;

  //get column list
  $query = "describe $table_name";
  $result = mysqli_query($mysqli,$query);
  $rows = mysqli_fetch_all($result,MYSQLI_ASSOC);
  $columns = array();
  foreach ($rows as $row)
    if (strtolower($row["Field"])!="id")
      array_push($columns,$row["Field"]);
  $column_list = implode(",",$columns);

  //create temp table
  $temp_table = $table_name."_temporary_table";
  $query = 
  "create table $temp_table as ".
  "select * from $table_name where 1 group by $column_list";
  mysqli_query($mysqli,$query);

  //drop old table
  $query = "drop table $table_name";
  mysqli_query($mysqli,$query);

  //rename temp table to old table
  $query = "rename table $temp_table to $table_name";
  mysqli_query($mysqli,$query);
}

I found a method to "remove duplicated rows grouped by all except ID" but it's not purely MySQL, requires additional PHP code and damn long:

$mysqli = mysqli_connect(...);

function remove_duplicated_rows($table_name) {
  global $mysqli;

  //get column list
  $query = "describe $table_name";
  $result = mysqli_query($mysqli,$query);
  $rows = mysqli_fetch_all($result,MYSQLI_ASSOC);
  $columns = array();
  foreach ($rows as $row)
    if (strtolower($row["Field"])!="id")
      array_push($columns,$row["Field"]);
  $column_list = implode(",",$columns);

  //create temp table
  $temp_table = $table_name."_temporary_table";
  $query = 
  "create table $temp_table as ".
  "select * from $table_name where 1 group by $column_list";
  mysqli_query($mysqli,$query);

  //drop old table
  $query = "drop table $table_name";
  mysqli_query($mysqli,$query);

  //rename temp table to old table
  $query = "rename table $temp_table to $table_name";
  mysqli_query($mysqli,$query);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文