SQL重复使用不同的主键

时间:2010-01-07 08:19:58

标签: sql

我有一张表格:

id | firstname | lastname
---+-----------+----------
1  | alex      | marti
2  | mark      | finger
3  | alex      | marti
4  | ted       | port

需要以此格式返回firstnamelastname个重复项:

1  | alex      | marti
3  | alex      | marti

我尝试过select firstname, lastname from t group by firstname, lastname having count(*) > 1,但会返回类似

的内容
firstname | lastname
----------+----------
mark      | finger
alex      | marti
ted       | port

我需要重复的id,但当然select id, firstname, lastname from t group by id, firstname, lastname将不起作用。

有什么想法吗?感谢。

7 个答案:

答案 0 :(得分:3)

select a.* from t a,
(select first, last from t group by first, last having count(*) > 1) b
where a.first = b.first and a.last = b.last

答案 1 :(得分:2)

您需要汇总ID。如果你只需要其中一个的ID,比如删除,你可以这样做:

select max(id) id, firstname, lastname from t group by firstname, lastname having count(*) > 1

如果你想要两个id并知道永远不会超过2,你可以做以下事情:

select min(id) minid, max(id) maxid, firstname, lastname from t group by firstname, lastname having count(*) > 1

如果您想要所有重复项及其ID,则必须使用派生表,如Nitin Midha的答案。

答案 2 :(得分:1)

Select Id, First_Name, Last_Name
FROM
(
Select Id, First_Name, Last_Name,
Count() Over (Partition By First_Name,Last_Name) Count
From Emp
) AS T
Where T.Count > 1

答案 3 :(得分:1)

select id, firstname, lastname
from table t
where exists (select 1
from table t2
where t2.firstname = t.firstname
and t2.lastname = t.lastname
and t2.id <> t.id)

答案 4 :(得分:0)

我遇到了同样的问题,这就是我解决它的方法。 首先,我使用以下查询识别了dup:

 SELECT COUNT(*) as num, ID, Firstname, Lastname FROM TableA GROUP BY ID, Firstname, Lastname;

然后我创建了一个临时表。叫做TempTableA 它与TableA和名为Dups的额外列具有相同的列,您将进一步了解原因。

然后我做了以下插入:

INSERT INTO TempTableA(Dups, ID, Firstname, Lastname) SELECT COUNT(*) as num, ID, Firstname, Lastname FROM TableA GROUP BY ID, Firstname, Lastname having count(*)>=1;

到目前为止,您可能知道为什么我们添加了一个名为dups的额外列。 anywho ..

之后我做了以下删除声明:

DELETE FROM TableA Where ID NOT IN (SELECT t.ID  FROM TempTableA t);

为我完成工作的presto删除了其余的重复项。

它不是一步到位的过程,但确实做得很好。

注意:您需要将tableA更改为您拥有的正确名称以及列名称才能使其正常工作。 如果您遇到任何问题,请告诉我。

答案 5 :(得分:0)

--Remove Duplicate Rows with different ID SQL SERVER

CREATE TABLE #TempTable
(
    Id        int,
    Firstname varchar(20),
    Lastname  varchar(20)
)

INSERT INTO #TempTable( Id, Firstname, Lastname) SELECT min(Id)as Id, Firstname, Lastname 
FROM UserTable 
GROUP BY  Firstname, Lastname

delete from UserTable where Id not in(select Id from #TempTable)

drop #TempTable

答案 6 :(得分:0)

您可以执行以下操作来显示所有 id 列值

#include <stdio.h>
#include <string.h>

void factorial_in_columns(int col_num, int width, int max_fact) {
    int col_width = width / col_num; /* the width of a column */
    int fact_offset = max_fact / col_num; /* calculation distance */

    char col_buffer[col_width]; /* buffer of a column */
    char line_buffer[width + 1]; /* buffer of a line */

    long factorial;
    int i, j, k, x, len;

    for(i = 1; i <= fact_offset; ++i) {
        for(j = 0; j < col_num; ++j) {
            x = (i + (j * fact_offset)); /* current number */

            for(factorial = 1, k = 1; k <= x; ++k)
                factorial *= k;

            /* convert the result to string and append it to the line buffer */
            snprintf(col_buffer, col_width, "%d = %ld", x, factorial);
            sprintf(line_buffer + (col_width * j), "%-*s", col_width, col_buffer);
        }            
        printf("%s\n", line_buffer);
    }
}

int main() {
    factorial_in_columns(4, 120, 20);
    return 0;
}

这应该显示如下:

SELECT GROUP_CONCAT(DISTINCT id SEPARATOR ',') AS ids, firstname, lastname FROM t GROUP BY firstname, lastname HAVING COUNT(*) > 1