The mysqlselect statement is used to query the result set. how can we sort the result set? We can use the orderby statement of mysql to sort the result set. This article introduces the sorting of the select query result set by mysqlorderby to the coders. For more information, see. Mysql order by syntax:
SELECT field1, field2,...fieldN table_name1, table_name2...ORDER BY field1, [field2...] [ASC [DESC]]
Note:
- You can use any field as the sorting condition to return the sorted query results.
- You can set multiple fields for sorting.
- You can use the ASC or DESC keyword to set the query results in ascending or descending order. By default, it is in ascending order.
- You can add the WHERE... LIKE clause to set conditions.
Mysql order by instance
First, create a table:
mysql> CREATE TABLE Employee( -> id int, -> first_name VARCHAR(15), -> last_name VARCHAR(15), -> start_date DATE, -> end_date DATE, -> salary FLOAT(8,2), -> city VARCHAR(10), -> description VARCHAR(15) -> );Query OK, 0 rows affected (0.03 sec)
Insert data into the table:
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,salary,City, Description) ->values (1,'Jason', 'Martin', '19960725', '20060725', 1234.56, 'Toronto', 'Programmer');Query OK, 1 row affected (0.02 sec)mysql>mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,salary,City, Description) ->values(2,'Alison', 'Mathews', '19760321', '19860221', 6661.78, 'Vancouver','Tester');Query OK, 1 row affected (0.00 sec)mysql>mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,salary,City, Description) ->values(3,'James', 'Smith', '19781212', '19900315', 6544.78, 'Vancouver','Tester');Query OK, 1 row affected (0.00 sec)mysql>mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,salary,City, Description) ->values(4,'Celia', 'Rice', '19821024', '19990421', 2344.78, 'Vancouver','Manager');Query OK, 1 row affected (0.00 sec)mysql>mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,salary,City, Description) ->values(5,'Robert', 'Black', '19840115', '19980808', 2334.78, 'Vancouver','Tester');Query OK, 1 row affected (0.00 sec)mysql>mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,salary,City, Description) ->values(6,'Linda', 'Green', '19870730', '19960104', 4322.78,'New York', 'Tester');Query OK, 1 row affected (0.00 sec)mysql>mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,salary,City, Description) ->values(7,'David', 'Larry', '19901231', '19980212', 7897.78,'New York','Manager');Query OK, 1 row affected (0.00 sec)mysql>mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,salary,City, Description) ->values(8,'James', 'Cat', '19960917', '20020415', 1232.78,'Vancouver', 'Tester');Query OK, 1 row affected (0.02 sec)
Query data and sort the result set:
mysql> SELECT first_name, start_date FROM employee ORDER BY start_date;+------------+------------+| first_name | start_date |+------------+------------+| Alison | 1976-03-21 || James | 1978-12-12 || Celia | 1982-10-24 || Robert | 1984-01-15 || Linda | 1987-07-30 || David | 1990-12-31 || Jason | 1996-07-25 || James | 1996-09-17 |+------------+------------+8 rows in set (0.02 sec)