Articulu Directory
MySQLClassificazione condizionale multi-campu ?MySQL ordine per dichjarazione / funzione di dumanda discendente è ascendente
sorta MySQL
Sapemu di utilizà a dichjarazione SQL SELECT per leghje e dati da a tabella MySQL.
Se avemu bisognu di sorte i dati di lettura, pudemu usà MySQL ORDINU DI clause per stabilisce u campu chì vulete sorte da quale modu, è poi rinvià i risultati di ricerca.
grammatica
U seguitu hè una dichjarazione SQL SELECT chì usa a clause ORDER BY per sorte e dati di a dumanda prima di rinvià i dati:
SELECT field1, field2,...fieldN table_name1, table_name2... ORDER BY field1, [field2...] [ASC [DESC]]
- Pudete utilizà qualsiasi campu cum'è una cundizione di sorte per rinvià i risultati di ricerca ordinati.
- Pudete stabilisce parechje campi per sorte.
- Pudete aduprà e parolle chjave ASC o DESC per stabilisce i risultati di a quistione per esse ordinati in ordine ascendente o descendente.Per automaticamente, hè in ordine crescente.
- Pudete aghjunghje WHERE ... LIKE clause per stabilisce e cundizioni.
Utilizà a clause ORDER BY in u prompt di cumanda
I seguenti utilizanu a clause ORDER BY in a dichjarazione SQL SELECT per leghje i dati in a tabella di dati MySQL chenweiliang_tbl:
Istanza
Pruvate l'esempii sottu è i risultati seranu ordinati in ordine crescente è decrescente.
sorta SQL
mysql> use chenweiliang; Database changed mysql> SELECT * from chenweiliang_tbl ORDER BY submission_date ASC; +-----------+---------------+---------------+-----------------+ | chenweiliang_id | chenweiliang_title | chenweiliang_author | submission_date | +-----------+---------------+---------------+-----------------+ | 3 | 学习 Java | chenweiliang.com | 2015-05-01 | | 4 | 学习 Python | chenweiliang.com | 2016-03-06 | | 1 | 学习 PHP | 陈沩亮博客 | 2017-04-12 | | 2 | 学习 MySQL | 陈沩亮博客 | 2017-04-12 | +-----------+---------------+---------------+-----------------+ 4 rows in set (0.01 sec) mysql> SELECT * from chenweiliang_tbl ORDER BY submission_date DESC; +-----------+---------------+---------------+-----------------+ | chenweiliang_id | chenweiliang_title | chenweiliang_author | submission_date | +-----------+---------------+---------------+-----------------+ | 1 | 学习 PHP | 陈沩亮博客 | 2017-04-12 | | 2 | 学习 MySQL | 陈沩亮博客 | 2017-04-12 | | 4 | 学习 Python | chenweiliang.com | 2016-03-06 | | 3 | 学习 Java | chenweiliang.com | 2015-05-01 | +-----------+---------------+---------------+-----------------+ 4 rows in set (0.01 sec)
Leghjite tutte e dati in a tavola di chenweiliang_tbl è sorte in ordine crescente da u campu submission_date.
Utilizà a clause ORDER BY in un script PHP
Pudete utilizà a funzione PHP mysqli_query () è u listessu cumandamentu SQL SELECT cù una clause ORDER BY per piglià e dati.
Questa funzione hè aduprata per eseguisce cumandamenti SQL è poi pruduce tutte e dati interrugati attraversu a funzione PHP mysqli_fetch_array ().
Istanza
Pruvate l'esempiu seguitu, i dati dumandati sò tornati in ordine decrescente di u campu submission_date.
MySQL ORDER BY test:
<?
php
$dbhost = 'localhost:3306'; // 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 chenweiliang_id, chenweiliang_title,
chenweiliang_author, submission_date
FROM chenweiliang_tbl
ORDER BY submission_date ASC';
mysqli_select_db( $conn, 'chenweiliang' );
$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, MYSQL_ASSOC))
{
echo "<tr><td> {$row['chenweiliang_id']}</td> ".
"<td>{$row['chenweiliang_title']} </td> ".
"<td>{$row['chenweiliang_author']} </td> ".
"<td>{$row['submission_date']} </td> ".
"</tr>";
}
echo '</table>';
mysqli_close($conn);
?>Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) hà spartutu "MySQL multi-field conditional sorting? Ordine MySQL per dichjarazione / funzione di dumanda discendente è ascendente", chì hè utile per voi.
Benvenuti à sparte u ligame di stu articulu:https://www.chenweiliang.com/cwl-476.html
