SQL查询:删除不是主键的列中的重复项[重复]

时间:2022-09-23 12:28:13

This question already has an answer here:

这个问题在这里已有答案:

I have a table with multiple columns, namely, Title, Url, Description and an integer Id which serves as the primary key.

我有一个包含多列的表,即Title,Url,Description和一个用作主键的整数Id。

In the tables, there are multiple entries with the same title but different url, description and of course different Ids.

在表格中,有多个条目具有相同的标题,但不同的网址,描述和当然不同的ID。

What would be the SQL query to obtain the rows that have unique Titles. With rows with the same title, it doesnt matter which row is return.

什么是SQL查询来获取具有唯一标题的行。对于具有相同标题的行,返回哪一行无关紧要。

1 个解决方案

#1


1  

You can use a query like this:

您可以使用如下查询:

SELECT t1.Title, t1.Url, t1.Description, t1.id
FROM mytable AS t1
JOIN (
   SELECT MIN(id) AS min_id
   FROM mytable
   GROUP BY title
) AS t2 ON t1.id = t2.min_id

This will pick the record having the minimum id for each distinct value of Title.

这将选择具有标题的每个不同值的最小id的记录。

#1


1  

You can use a query like this:

您可以使用如下查询:

SELECT t1.Title, t1.Url, t1.Description, t1.id
FROM mytable AS t1
JOIN (
   SELECT MIN(id) AS min_id
   FROM mytable
   GROUP BY title
) AS t2 ON t1.id = t2.min_id

This will pick the record having the minimum id for each distinct value of Title.

这将选择具有标题的每个不同值的最小id的记录。