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.