1994.4.16
by Marisa Plumb
new function and clause files, some placeholders for right now |
1 |
Order By
|
1994.4.19
by Marisa Plumb
new orderby doc |
2 |
========
|
3 |
||
4 |
The ORDER BY keyword is used to sort the result-set by column; by default, it sorts the records in ascending order. |
|
5 |
||
2165.2.5
by Andrew Hutchings
Make fixes to temporal docs |
6 |
SQL ORDER BY Syntax: |
7 |
||
8 |
.. code-block:: mysql |
|
1994.4.19
by Marisa Plumb
new orderby doc |
9 |
|
10 |
SELECT column_name(s) |
|
11 |
FROM table_name |
|
12 |
ORDER BY column_name(s) ASC|DESC; |
|
13 |
||
14 |
**ORDER BY Example**
|
|
15 |
||
16 |
The "Persons" table: |
|
17 |
||
18 |
+---------+------------+----------+----------+--------+ |
|
19 |
|Id |LastName |FirstName |Address | City | |
|
20 |
+=========+============+==========+==========+========+ |
|
21 |
| 1 | Larson | Sue |3 Cherry | Chicago|
|
|
22 |
+---------+------------+----------+----------+--------+ |
|
23 |
| 2 | Roberts | Teri |21 Brown | Chicago|
|
|
24 |
+---------+------------+----------+----------+--------+ |
|
25 |
| 3 | Peterson | Kari |30 Mell | Reno |
|
|
26 |
+---------+------------+----------+----------+--------+ |
|
27 |
||
2165.2.5
by Andrew Hutchings
Make fixes to temporal docs |
28 |
To select all the persons from the table above, and also sort them by their last name, use the following SELECT statement: |
29 |
||
30 |
.. code-block:: mysql |
|
1994.4.19
by Marisa Plumb
new orderby doc |
31 |
|
32 |
SELECT * FROM Persons |
|
33 |
ORDER BY LastName; |
|
34 |
||
35 |
The result-set will look like this: |
|
36 |
||
37 |
+---------+------------+----------+----------+--------+ |
|
38 |
|Id |LastName |FirstName |Address | City | |
|
39 |
+=========+============+==========+==========+========+ |
|
40 |
| 1 | Larson | Sue |3 Cherry | Chicago|
|
|
41 |
+---------+------------+----------+----------+--------+ |
|
42 |
| 3 | Peterson | Kari |30 Mell | Reno |
|
|
43 |
+---------+------------+----------+----------+--------+ |
|
44 |
| 2 | Roberts | Teri |21 Brown | Chicago|
|
|
45 |
+---------+------------+----------+----------+--------+ |
|
46 |
||
1994.4.112
by Marisa Plumb
current time function fixes |
47 |
Without using "ORDERBY" in the following query, the result-set will be non-deterministic, and could returned matching rows in a different order for each query. |
48 |
||
2165.2.5
by Andrew Hutchings
Make fixes to temporal docs |
49 |
ORDER BY DESC can be used to reverse the order of the result set. |
50 |
||
51 |
.. code-block:: mysql |
|
1994.4.19
by Marisa Plumb
new orderby doc |
52 |
|
53 |
SELECT * FROM Persons |
|
54 |
ORDER BY LastName DESC; |
|
1994.5.18
by Stewart Smith
add some FIXMEs to ORDER BY docs about what should be added. |
55 |
|
2165.2.3
by Andrew Hutchings
Add favicon to docs |
56 |
|
57 |
.. todo:: |
|
58 |
||
59 |
add something about how ORDER BY is executed. index scan vs filesort |