Ver Mensaje Individual
  #2 (permalink)  
Antiguo 06/10/2010, 09:29
Avatar de huesos52
huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: ¿ Como ordenar un varchar como si fueran horas ?

Código MySQL:
Ver original
  1. mysql> SELECT *FROM horas_1;
  2. +-------+
  3. | hora  |
  4. +-------+
  5. | 9     |
  6. | 9:30  |
  7. | 10:30 |
  8. | 10    |
  9. | 8:30  |
  10. | 8     |
  11. | 11    |
  12. | 11:30 |
  13. +-------+
  14. 8 rows in set (0.00 sec)
  15.  
  16. mysql> SELECT *FROM horas_1 order by cast(if(length(hora)<=2,concat(hora,':00'),hora) as time);
  17. +-------+
  18. | hora  |
  19. +-------+
  20. | 8     |
  21. | 8:30  |
  22. | 9     |
  23. | 9:30  |
  24. | 10    |
  25. | 10:30 |
  26. | 11    |
  27. | 11:30 |
  28. +-------+
  29. 8 rows in set (0.00 sec)
  30.  
  31. mysql>

sin embargo, te recoiendo manejar el tipo de dato time y mas bien organizar el formato definitivo de esta misma forma.
algo como
Código MySQL:
Ver original
  1. UPDATE horas_1 SET hora= if(length(hora)<=2,concat(hora,':00');

De esta forma el ordenamiento es directo.

saludos
__________________
Without data, You are another person with an opinion.
W. Edwads Deming