Mega Code Archive

 
Categories / MySQL / Select Clause
 

Sort the column by the order in which colors occur in the rainbow

mysql> mysql> CREATE TABLE color (name CHAR(10)); Query OK, 0 rows affected (0.01 sec) mysql> INSERT INTO color (name) VALUES ('blue'),('green'),     -> ('indigo'),('orange'),('red'),('violet'),('yellow'); Query OK, 7 rows affected (0.00 sec) Records: 7  Duplicates: 0  Warnings: 0 mysql> mysql> mysql> SELECT name FROM color     -> ORDER BY     -> FIELD(name,'red','orange','yellow','green','blue','indigo','violet'); +--------+ | name   | +--------+ | red    | | orange | | yellow | | green  | | blue   | | indigo | | violet | +--------+ 7 rows in set (0.00 sec) mysql> mysql> drop table color; Query OK, 0 rows affected (0.00 sec)