Mega Code Archive

 
Categories / MySQL / Select Clause
 

Sort direction

mysql> mysql> mysql> CREATE TABLE Item     -> (     ->   position       INT             PRIMARY KEY,     ->   title  VARCHAR(25)     NOT NULL,     ->   year           INT             NOT NULL     -> ); Query OK, 0 rows affected (0.00 sec) mysql> mysql> # insert 5 records into the "top_5" table mysql> INSERT INTO Item (position, title, year)         VALUES (1, "Citizen Kane", 1941); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO Item (position, title, year)         VALUES (2, "Casablanca",1942); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO Item (position, title, year)         VALUES (3, "The Godfather", 1972); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO Item (position, title, year)         VALUES (4, "Gone With The Wind",1939); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO Item (position, title, year)         VALUES (5, "Lawrence Of Arabia", 1962); Query OK, 1 row affected (0.00 sec) mysql> mysql> # show all data in "Item" in descending position order mysql> SELECT * FROM Item ORDER BY position DESC; +----------+--------------------+------+ | position | title              | year | +----------+--------------------+------+ |        5 | Lawrence Of Arabia | 1962 | |        4 | Gone With The Wind | 1939 | |        3 | The Godfather      | 1972 | |        2 | Casablanca         | 1942 | |        1 | Citizen Kane       | 1941 | +----------+--------------------+------+ 5 rows in set (0.00 sec) mysql> mysql> # show all data in "Item" in ascending year order mysql> SELECT * FROM Item ORDER BY year ASC; +----------+--------------------+------+ | position | title              | year | +----------+--------------------+------+ |        4 | Gone With The Wind | 1939 | |        1 | Citizen Kane       | 1941 | |        2 | Casablanca         | 1942 | |        5 | Lawrence Of Arabia | 1962 | |        3 | The Godfather      | 1972 | +----------+--------------------+------+ 5 rows in set (0.00 sec) mysql> mysql> # show all data in "Item" in alphabetical order mysql> SELECT * FROM Item ORDER BY title ASC; +----------+--------------------+------+ | position | title              | year | +----------+--------------------+------+ |        2 | Casablanca         | 1942 | |        1 | Citizen Kane       | 1941 | |        4 | Gone With The Wind | 1939 | |        5 | Lawrence Of Arabia | 1962 | |        3 | The Godfather      | 1972 | +----------+--------------------+------+ 5 rows in set (0.00 sec) mysql> mysql> # delete this sample table mysql> DROP TABLE Item; Query OK, 0 rows affected (0.00 sec)