Summarize some knowledge points of vue3: MySQL sorting

MySQL sort

We know to use SQL SELECT statement to read data from MySQL table.

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

grammar

The following is the SQL SELECT statement that uses the ORDER BY clause to sort the query data before returning the data:


SELECT field1, field2,...fieldN FROM table_name1, table_name2...
ORDER BY field1 [ASC [DESC][默认 ASC]], [field2...] [ASC [DESC][默认 ASC]]
  • You can use any field as a sorting condition to return sorted query results.
  • You can set multiple fields to sort by.
  • You can use the ASC or DESC keywords to set the query results in ascending or descending order. By default, it is sorted in ascending order.
  • You can add WHERE...LIKE clauses to set conditions.

Using the ORDER BY clause in the command prompt

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

example

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

SQL sorting

mysql> use RUNOOB;
Database changed
mysql> SELECT * from kxdang_tbl ORDER BY submission_date ASC;
+-----------+---------------+---------------+-----------------+
| kxdang_id | kxdang_title  | kxdang_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 3         | 学习 Java   | RUNOOB.COM    | 2015-05-01      |
| 4         | 学习 Python | RUNOOB.COM    | 2016-03-06      |
| 1         | 学习 PHP    | 菜鸟教程  | 2017-04-12      |
| 2         | 学习 MySQL  | 菜鸟教程  | 2017-04-12      |
+-----------+---------------+---------------+-----------------+
4 rows in set (0.01 sec)
 
mysql> SELECT * from kxdang_tbl ORDER BY submission_date DESC;
+-----------+---------------+---------------+-----------------+
| kxdang_id | kxdang_title  | kxdang_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 1         | 学习 PHP    | 菜鸟教程  | 2017-04-12      |
| 2         | 学习 MySQL  | 菜鸟教程  | 2017-04-12      |
| 4         | 学习 Python | RUNOOB.COM    | 2016-03-06      |
| 3         | 学习 Java   | RUNOOB.COM    | 2015-05-01      |
+-----------+---------------+---------------+-----------------+
4 rows in set (0.01 sec)

Read all the data in the kxdang_tbl table and sort them in ascending order of the submission_date field.


Using ORDER BY clause in PHP script

You can use the PHP function mysqli_query() and the same SQL SELECT command with an ORDER BY clause to get data.

This function is used to execute the SQL command, and then output all the query data through the PHP function mysqli_fetch_array().

example

Try the following example, the query data is sorted in descending order of the submission_date field and returned.

MySQL ORDER BY test:

<?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 kxdang_id, kxdang_title, 
        kxdang_author, submission_date
        FROM kxdang_tbl
        ORDER BY  submission_date ASC';
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('无法读取数据: ' . mysqli_error($conn));
}
echo '<h2>菜鸟教程 MySQL ORDER BY 测试<h2>';
echo '<table border="1"><tr><td>教程 ID</td><td>标题</td><td>作者</td><td>提交日期</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row['kxdang_id']}</td> ".
         "<td>{$row['kxdang_title']} </td> ".
         "<td>{$row['kxdang_author']} </td> ".
         "<td>{$row['submission_date']} </td> ".
         "</tr>";
}
echo '</table>';
mysqli_close($conn);
?>

Guess you like

Origin blog.csdn.net/weixin_46626339/article/details/131115111