Heim >Datenbank >MySQL-Tutorial >mysql order by 对select查询结果集排序

mysql order by 对select查询结果集排序

WBOY
WBOYOriginal
2016-06-01 09:56:491408Durchsuche

mysql order by 语法:

<code class="language-sql">SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]</code>

说明: 

  • 你可以使用任何字段来作为排序的条件,从而返回排序后的查询结果。
  • 你可以设定多个字段来排序。
  • 你可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列。 默认情况下,它是按升排列。
  • 你可以添加 WHERE...LIKE 子句来设置条件。

 

mysql order by实例

首先创建一张表:

<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>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn