English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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 which field and sort method you want to use, and then return the search results.
Below is an example of an SQL SELECT statement using 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 a sorting condition to return the sorted query results.
You can sort by multiple fields.
You can use the ASC or DESC keyword to set whether the query results are sorted in ascending or descending order. By default, it is sorted in ascending order.
You can add a WHERE...LIKE clause to set conditions.
Below, the SQL SELECT statement will use the ORDER BY clause 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 | Aprender PHP | oldtoolbag.com | 2018-04-14 | | 2 | Aprender MySQL | oldtoolbag.com | 2018-04-14 | | 3 | Tutoriales JAVA | oldtoolbag.com | 2018-04-14 | | 4 | Aprender 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 | Aprender Python | oldtoolbag.com | 2019-06-08 | | 1 | Aprender PHP | oldtoolbag.com | 2018-04-14 | | 2 | Aprender MySQL | oldtoolbag.com | 2018-04-14 | | 3 | Tutoriales JAVA | oldtoolbag.com | 2018-04-14 | +----------+-------------+--------------+-----------------+ 4 filas en conjunto (0.02 sec)
Leer w3Todos los datos de la tabla codebox_tbl ordenados en orden ascendente por el campo $submission_date.
Usted puede usar la función mysqli_query() de PHP y el comando SELECT con la cláusula ORDER BY para obtener datos.
Esta función se utiliza para ejecutar el comando SQL y luego mostrar todos los datos de la consulta mediante la función PHP mysqli_fetch_array().
Intente con el siguiente ejemplo, el conjunto de datos devueltos después de la consulta se ordena en orden descendente por el campo $submission_date.
<?php $dbhost = 'localhost'; // Dirección del servidor de MySQL $dbuser = 'root'; // Nombre de usuario de MySQL $dbpass = '123456'; // Nombre de usuario y contraseña de MySQL $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if (!$conn) { die('Fallo de conexión: ' . mysqli_error($conn)); } // Establecer la codificación para evitar la desordenación de los caracteres chinos 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('No se puede leer los datos: ' . mysqli_error($conn)); } echo '<h2">Base de datos MySQL ORDER BY prueba<h2">;</ echo '<table border="1><tr><td> ID de tutorial</td><td>Título</td><td>Autor</td><td>Fecha de presentación</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); ?>
El resultado de la salida se muestra como se ilustra a continuación: