Maison >base de données >tutoriel mysql >mysql order by 对select查询结果集排序
<code class="language-sql">SELECT field1, field2,...fieldN table_name1, table_name2... ORDER BY field1, [field2...] [ASC [DESC]]</code>
说明:
首先创建一张表:
<code class="language-sql">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)</code>
向表中插入若干数据:
<code class="language-sql">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)</code>
查询数据并对结果集排序:
<code class="language-sql">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)</code>