2011-04-27 35 views
6

tengo, SELECT DISTINCT (first),second,third FROM tableDISTINCT en varias columnas

y yo no sólo quieren que la primera pueda ser distinto y el segundo a ser distinto a la tercera, pero quedarse sin DISTINCT, i tryed así.

SELECT DISTINCT (first,second),third FROM table 

Y un par de cosas más, pero no funcionó.

Respuesta

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 
     ) 

Cree un índice en (first, second, third) para que esto funcione rápidamente.

+1

Esta es una forma muy interesante de hacer esto. –

Cuestiones relacionadas