排序
语法:
SELECT field1,field2,...fieldN FROM table_name1,table_name2... ORDER BY field1[ASC[DESC][默认 ASC]],[field2...][ASC[DESC][默认 ASC]]
- 可以使用个人和字段来作为排序条件,从而返回排序后的查询结果
- 设定多个字段来排序
- ASC(升序),DESC(降序)
- 可以添加WHERE...LIKE来设置条件
在命令提示符中使用ORDER BY子句
在PHP脚本中使用ORDER BY子句
<?php
header("content-type:text/html;charset=utf-8");
$dbhost='localhost';
$dbuser='root';
$dbpass='root';
$conn=mysqli_connect($dbhost,$dbuser,$dbpass);
if(!$conn)
{
die('连接失败:'.mysqli_error($conn));
}
mysqli_query($conn,"set names utf8");
$sql='SELECT id,title,author,submission_date FROM bird_tbl ORDER BY submission_date ASC';
mysqli_select_db($conn,'bird');
$retval=mysqli_query($conn,$sql);
if(!$retval)
{
die('无法读取数据:'.mysqli_error($conn));
}
echo "ORDER BY";
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['id']}</td>".
"<td>{$row['title']}</td>".
"<td>{$row['author']}</td>".
"<td>{$row['submission_date']}</td>".
"</tr>";
}
echo '</table>';
mysqli_close($conn);
?>