articulus Directory
Ordinatio multi-camporum MySQL ? Quaestio/functio MySQL ORDER BY ordine descendente/ascendente
Ordinatio MySQL
Scimus uti SQL SELECT constitutione data ex tabula MySQL legere.
Si data lecta ordinare necesse est, clausulam ORDER BY MySQL uti possumus ad specificandum quem campum et modum ordinandi uti velimus, deinde eventus investigationis reddendos.
grammatica
Exemplum sequens est enuntiationis SQL SELECT quae clausulam ORDER BY adhibet ad data interrogata ordinanda antequam ea reddat:
SELECT field1, field2,...fieldN table_name1, table_name2... ORDER BY field1, [field2...] [ASC [DESC]]
- Quolibet agro ut condicione ordinationis uti potes ad eventus interrogationum ordinatos reddendos.
- Plura spatia ad ordinandum constituere potes.
- Verbis clavibus ASC vel DESC uti potes ad eventus interrogationum ordine ascendente vel descendente ordinandos. Implicite, ordine ascendente ordinantur.
- Clausulam "WHERE...LIKE" addere potes ad condiciones statuendas.
Clausula "ORDER BY" in promptu mandatorum utere.
Sequentia clausulam ORDER BY in enuntiato SQL SELECT ad legendum data ex tabula MySQL chenweiliang_tbl utentur:
Exempli gratia
Exempla sequentia experire; eventus ordine crescente et descendente ordinabuntur.
Ordinatio 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)
Omnia data ex tabula "chenweiliang_tbl" lege et ea ordine ascendente secundum agrum "submission_date" dispone.
Usus clausulae ORDER BY in scriptis PHP
Functionem PHP `mysqli_query()` et idem mandatum SQL `SELECT` cum clausula `ORDER BY` ad notitias recuperandas uti potes.
Munus hoc adhibitum est ad SQL mandata exsequendam ac deinde omnia data quaestionis per PHP functionem mysqli_fetch_array output().
Exempli gratia
Exemplum sequens experire; data interrogata ordine descendente agri `submission_date` redduntur.
ORDINE MySQL 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);
?>Speramus te utilem fore articulum "MySQL Multi-Field Condition Sorting? MySQL ORDER BY Descending/Ascending Order Query Statement/Function" in diario Chen Weiliang ( https://www.chenweiliang.com/ ) communicatum.
Libere hunc articulum communicare potes: https://www.chenweiliang.com/cwl-476.html
