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
  • 75,655
  • 22
  • 151
  • 221
weardstuff
  • 781
  • 5
  • 15
  • 22

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
  • 413,100
  • 91
  • 616
  • 614
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,585
  • 11
  • 60
  • 105