runoops.com

PHP MySQL Order By 关键词

ORDER BY 关键词用于对记录集中的数据进行排序。


ORDER BY 关键词

ORDER BY 关键词用于对记录集中的数据进行排序。

ORDER BY 关键词默认对记录进行升序排序。

如果你想降序排序,请使用 DESC 关键字。

语法

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

如需学习更多关于 SQL 的知识,请访问我们的 SQL 教程

下面的实例选取 "myguests" 表中存储的所有数据,并根据 "reg_date" 列对结果进行排序:


使用 MySQLi

MySQLi - 面向对象:

实例

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "mydb";
 
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
 
$sql = "SELECT id, firstname, lastname FROM myguests ORDER BY reg_date DESC";

//创建预处理语句
$stmt  = $conn->prepare($sql);

//执行查询
$stmt->execute();

//获取查询结果
$result = $stmt->get_result();

 
if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 结果";
}
$conn->close();
?>

MySQLi - 面向过程:

实例

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "mydb";

// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
 
$sql = "SELECT id, firstname, lastname FROM myguests ORDER BY reg_date DESC";

$stmt = mysqli_prepare($conn, $sql);

//执行查询
mysqli_stmt_execute($stmt);

//获取结果
$result = mysqli_stmt_get_result($stmt);
 
if (mysqli_num_rows($result) > 0) {
    // 输出数据
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 结果";
}
 
mysqli_close($conn);
?>

使用 PDO (+ 预处理)

以下实例使用了预处理语句。

选取了 myguests 表中的 id, firstname 和 lastname 字段,并放到 HTML 表格中:

实例

<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";
 
class TableRows extends RecursiveIteratorIterator {
    function __construct($it) { 
        parent::__construct($it, self::LEAVES_ONLY); 
    }
 
    function current() {
        return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
    }
 
    function beginChildren() { 
        echo "<tr>"; 
    } 
 
    function endChildren() { 
        echo "</tr>" . "\n";
    } 
} 
 
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "mydbpdo";
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT id, firstname, lastname FROM myguests ORDER BY reg_date DESC"); 
    $stmt->execute();
 
    // 设置结果集为关联数组
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>

根据两列进行排序

可以根据多个列进行排序。当按照多个列进行排序时,只有第一列的值相同时才使用第二列:

SELECT column_name(s)
FROM table_name
ORDER BY column1, column2

Captcha Code

0 笔记

分享笔记

Inline Feedbacks
View all notes