English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MySQL- Sortierung

We know that we can read data from MySQL tables using SQL SELECT statements.

If we need to sort the data we read, we can use MySQL's ORDER BY clauses to set the field and sorting method you want to sort by, and then return the search results.

Syntax

The following SQL SELECT statement uses the ORDER BY clause to sort the query data and then return the data:

SELECT field1, field2,...fieldN FROM table_name1, table_name2...
ORDER BY field1 [ASC [DESC][default ASC]], [field2...] [ASC [DESC][default ASC]]
  • You can use any field as the sorting condition to return the sorted query results.

  • You can set multiple fields to sort.

  • You can use the ASC or DESC keyword to set the query results to be sorted in ascending or descending order. By default, it is sorted in ascending order.

  • You can add a WHERE...LIKE clause to set conditions.

Use the ORDER BY clause in the command prompt

The following will use the ORDER BY clause in the SQL SELECT statement to read the MySQL table w3Data in codebox_tbl:

在线示例

Try the following examples, and the results will be sorted in ascending and descending order.

mysql> use w3codebox;
Database changed
MariaDB [w3codebox] > SELECT * from w3codebox_tbl ORDER BY submission_date ASC;
+----------+-------------+--------------+-----------------+
| w3codebox_id | w3codebox_title | w3codebox_author | submission_date |
+----------+-------------+--------------+-----------------+
|        1 | 学习 PHP         | oldtoolbag.com       | 2018-04-14      |
|        2 | 学习 MySQL       | oldtoolbag.com       | 2018-04-14      |
|        3 | JAVA 教程       | oldtoolbag.com       | 2018-04-14      |
|        4 | 学习 Python      | oldtoolbag.com       | 2019-06-08      |
+----------+-------------+--------------+-----------------+
4 rows in set (0.00 sec)
MariaDB [w3codebox] > SELECT * from w3codebox_tbl ORDER BY submission_date DESC;
+----------+-------------+--------------+-----------------+
| w3codebox_id | w3codebox_title | w3codebox_author | submission_date |
+----------+-------------+--------------+-----------------+
|        4 | 学习 Python      | oldtoolbag.com       | 2019-06-08      |
|        1 | 学习 PHP         | oldtoolbag.com       | 2018-04-14      |
|        2 | 学习 MySQL       | oldtoolbag.com       | 2018-04-14      |
|        3 | JAVA 教程       | oldtoolbag.com       | 2018-04-14      |
+----------+-------------+--------------+-----------------+
4 rows in set (0.02 sec)

读取 w3codebox_tbl 表中所有数据并按 submission_date 字段的升序排列。

在 PHP 脚本中使用 ORDER BY 子句

你可以使用 PHP 函数的 mysqli_query() 及相同的 SQL SELECT 带上 ORDER BY 子句的命令来获取数据。

该函数用于执行 SQL 命令,然后通过 PHP 函数 mysqli_fetch_array() 来输出所有查询的数据。

在线示例

尝试以下示例,查询后的数据按 submission_date 字段的降序排列后返回。

<?php
$dbhost = 'localhost';  // MySQL 服务器主机地址
$dbuser = 'root';            // MySQL 用户名
$dbpass = '123456';          // MySQL 用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if (!$conn)
{
    die('连接失败: ' . mysqli_error($conn));
}
// 设置编码,防止中文乱码
mysqli_query($conn, "set names utf8');
 
$sql = 'SELECT w3codebox_id, w3codebox_title, 
        w3codebox_author, submission_date
        FROM w3codebox_tbl
        ORDER BY submission_date ASC';
 
mysqli_select_db($conn, 'w3codebox');
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('Daten können nicht gelesen werden: ' . mysqli_error($conn));
}
echo '<h2">Grund教程网 MySQL ORDER BY Test<h2">;</
echo '<table border="1><tr><td>教程 ID</td><td>Titel</td><td>Autor</td><td>Einreichungsdatum</td></tr>';
while($row = mysqli_fetch_array($retval))
{
    echo "<tr><td>" {$row['w3codebox_id']}</td>".
         "<td>{$row['w3codebox_title']} </td>".
         "<td>{$row['w3codebox_author']} </td>".
         "<td>{$row['submission_date']} </td>".
         "</tr>";
}
echo '</table>';
mysqli_close($conn);
?>

Die Ausgabeergebnisse sind wie folgt dargestellt: