选择 table 的 ID,其中 3 列或更多列重复
Selecting the Ids of a table where 3 or more column are duplicates
我正在尝试 select 我的 table 中的 3 列的 ID,它们都包含相同的数据,但第一次出现重复项时除外。比如我的table是这样的:
Select * From Workers
+----+--------+--------+--------------+
| id | name |JobTitle| description |
+----+--------+--------+--------------+
| 1 | john |Plumber |Installs Pipes|
| 2 | mike | Doctor |Provides Meds |
| 3 | john |Plumber |Installs Pipes|
| 4 | john |Plumber |Installs Pipes|
| 5 | mike | Doctor |Provides Meds |
| 6 | mike | Doctor |Provides Meds |
+----+--------+--------+--------------+
我基本上想得到的是所有重复记录的 ID,期望出现重复的最低 ID 或第一个 ID。
SELECT t1.id
From workers t1, workers t2
Where t1.id > t2.Id and t1.name = t2.name and t1.jobTitle = t2.jobTitle and t1.description = t2.description;
我正在使用的 table 有数十万条记录,我已经尝试了上面的语句来获取我想要的 ID,但是由于 table 的大小,我得到了错误:
Error Code: 1054. Unknown column 't1.userId' in 'where clause'
我曾尝试增加 workbench 中的超时,但无济于事。
在这个例子中,我基本上是在尝试获取除 1 和 2 之外的所有 ID。我认为上面的查询会得到我正在寻找的东西,但事实并非如此,现在我不确定还能尝试什么。
非常感谢任何帮助。提前致谢。
错误消息与您的查询不匹配(查询中没有 userId
列)- 并且与 table.
的大小无关
无论如何,我会用 exists
:
过滤
select w.*
from workers w
where exists (
select 1
from workers w1
where
w1.name = w.name
and w1.jobTitle = w.jobTitle
and w1.description = w.description
and w1.id < w.id
)
为了性能,考虑在 (name, jobTitle, description, id)
上建立索引。
你可以用'INNER JOIN
SELECT DISTINCT t1.*
From workers t1
INNER JOIN workers t2 ON t1.name = t2.name and t1.jobTitle = t2.jobTitle and t1.description = t2.description
Where t1.id > t2.Id ;
但我不知道你是怎么收到消息的,看不到用户 ID
我正在尝试 select 我的 table 中的 3 列的 ID,它们都包含相同的数据,但第一次出现重复项时除外。比如我的table是这样的:
Select * From Workers
+----+--------+--------+--------------+
| id | name |JobTitle| description |
+----+--------+--------+--------------+
| 1 | john |Plumber |Installs Pipes|
| 2 | mike | Doctor |Provides Meds |
| 3 | john |Plumber |Installs Pipes|
| 4 | john |Plumber |Installs Pipes|
| 5 | mike | Doctor |Provides Meds |
| 6 | mike | Doctor |Provides Meds |
+----+--------+--------+--------------+
我基本上想得到的是所有重复记录的 ID,期望出现重复的最低 ID 或第一个 ID。
SELECT t1.id
From workers t1, workers t2
Where t1.id > t2.Id and t1.name = t2.name and t1.jobTitle = t2.jobTitle and t1.description = t2.description;
我正在使用的 table 有数十万条记录,我已经尝试了上面的语句来获取我想要的 ID,但是由于 table 的大小,我得到了错误:
Error Code: 1054. Unknown column 't1.userId' in 'where clause'
我曾尝试增加 workbench 中的超时,但无济于事。 在这个例子中,我基本上是在尝试获取除 1 和 2 之外的所有 ID。我认为上面的查询会得到我正在寻找的东西,但事实并非如此,现在我不确定还能尝试什么。
非常感谢任何帮助。提前致谢。
错误消息与您的查询不匹配(查询中没有 userId
列)- 并且与 table.
无论如何,我会用 exists
:
select w.*
from workers w
where exists (
select 1
from workers w1
where
w1.name = w.name
and w1.jobTitle = w.jobTitle
and w1.description = w.description
and w1.id < w.id
)
为了性能,考虑在 (name, jobTitle, description, id)
上建立索引。
你可以用'INNER JOIN
SELECT DISTINCT t1.*
From workers t1
INNER JOIN workers t2 ON t1.name = t2.name and t1.jobTitle = t2.jobTitle and t1.description = t2.description
Where t1.id > t2.Id ;
但我不知道你是怎么收到消息的,看不到用户 ID