6

I have, SELECT DISTINCT (first),second,third FROM table

AND i want not only the first to be DISTINCT and the second to be DISTINCT to but the third to stay without DISTINCT , i tryed like that.

SELECT DISTINCT (first,second),third FROM table

And couple more things but didnt worked.

outis
  • 72,188
  • 19
  • 145
  • 210
weardstuff
  • 741
  • 5
  • 14
  • 21

2 Answers2

6
SELECT  m.first, m.second, m.third -- and possibly other columns
FROM    (
        SELECT  DISTINCT  first, second
        FROM    mytable
        ) md
JOIN    mytable m
ON      m.id =
        (
        SELECT  id
        FROM    mytable mi
        WHERE   mi.first = md.first
                AND mi.second = md.second
        ORDER BY
                mi.first, mi.second, mi.third
        LIMIT 1
        )

Create an index on (first, second, third) for this to work fast.

Quassnoi
  • 398,504
  • 89
  • 603
  • 604
0

Have you seen this post?

Select distinct from multiple fields using sql

They seem very similar, maybe you could try something like that?

Hope this helps!

Community
  • 1
  • 1
lhan
  • 4,557
  • 10
  • 52
  • 104