列的重复记录

发布于 2024-12-19 06:19:15 字数 1291 浏览 1 评论 0原文

我试图在 col1 中获取某个 col2 值的重复值。

假设我有该表:

+----+------------+----------+
| id | col1       | col2     |
+----+------------+----------+
|  1 | 5          | 2        |
|  2 | 5          | 1        |
|  3 | 8          | 4        |
|  4 | 8          | 1        |
|  5 | 8          | 3        |
|  6 | 5          | 2        |
|  7 | 2          | 3        |
|  8 | 1          | 4        |
|  9 | 2          | 2        |
| 10 | 5          | 2        |
| 11 | 5          | 3        |
| 12 | 3          | 1        |
+----+------------+----------+

col2 = 1 时,我的查询应返回这些行:

+----+------------+----------+
| id | col1       | col2     |
+----+------------+----------+
|  1 | 5          | 2        |
|  6 | 5          | 2        |
| 10 | 5          | 2        |
| 11 | 5          | 3        |
|  3 | 8          | 4        |
|  5 | 8          | 3        |
+----+------------+----------+

我已经尝试过此查询,它对我来说效果很好:

SELECT 
DISTINCT b.* 
FROM table a,table b 
WHERE a.col1 = b.col1 AND a.col2 = 1 AND b.col2 != 1

如您所见,DISTINCT 对于一个包含 100k 条记录的巨大表来说是很糟糕的,而且它每天都在增长。

我需要所有值,因此无法使用 GROUP BY 子句。

寻找更好更快的解决方案。如果更好的话,我可以改变整个结构。

I'm trying to get duplicated values in col1 for a certain col2 value.

Suppose that I have that table:

+----+------------+----------+
| id | col1       | col2     |
+----+------------+----------+
|  1 | 5          | 2        |
|  2 | 5          | 1        |
|  3 | 8          | 4        |
|  4 | 8          | 1        |
|  5 | 8          | 3        |
|  6 | 5          | 2        |
|  7 | 2          | 3        |
|  8 | 1          | 4        |
|  9 | 2          | 2        |
| 10 | 5          | 2        |
| 11 | 5          | 3        |
| 12 | 3          | 1        |
+----+------------+----------+

My query should return these rows when col2 = 1:

+----+------------+----------+
| id | col1       | col2     |
+----+------------+----------+
|  1 | 5          | 2        |
|  6 | 5          | 2        |
| 10 | 5          | 2        |
| 11 | 5          | 3        |
|  3 | 8          | 4        |
|  5 | 8          | 3        |
+----+------------+----------+

I have tried this query and it works pretty well for me:

SELECT 
DISTINCT b.* 
FROM table a,table b 
WHERE a.col1 = b.col1 AND a.col2 = 1 AND b.col2 != 1

As you can see, DISTINCT is killing for a huge table with 100k records and it's daily growing.

I need all values so I can't use GROUP BY clause.

Looking for a better and faster solution. If its better, I can change the whole structure.

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

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

发布评论

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

评论(1

无可置疑 2024-12-26 06:19:15
SELECT a.* 
FROM table AS a 
WHERE col2 <> 1
  AND EXISTS
      ( SELECT *
        FROM table b
        WHERE b.col1 = a.col1 
          AND b.col2 = 1
      )
SELECT a.* 
FROM table AS a 
WHERE col2 <> 1
  AND EXISTS
      ( SELECT *
        FROM table b
        WHERE b.col1 = a.col1 
          AND b.col2 = 1
      )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文