列的重复记录
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)