{"id":3111,"date":"2025-03-22T00:14:54","date_gmt":"2025-03-21T16:14:54","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3111"},"modified":"2025-03-22T00:14:54","modified_gmt":"2025-03-21T16:14:54","slug":"php-mysql-order-by-%e5%85%b3%e9%94%ae%e8%af%8d","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/22\/php-mysql-order-by-%e5%85%b3%e9%94%ae%e8%af%8d\/","title":{"rendered":"PHP MySQL ORDER BY \u5173\u952e\u8bcd"},"content":{"rendered":"\n<p>\u5728 PHP \u4e2d\u4e0e MySQL \u4ea4\u4e92\u65f6\uff0c<code>ORDER BY<\/code> \u5173\u952e\u8bcd\u7528\u4e8e\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u6392\u5e8f\u53ef\u4ee5\u662f\u6309\u5347\u5e8f\uff08<code>ASC<\/code>\uff09\u6216\u964d\u5e8f\uff08<code>DESC<\/code>\uff09\u6392\u5217\u3002\u5b83\u901a\u5e38\u4e0e <code>SELECT<\/code> \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. ORDER BY \u5173\u952e\u8bcd\u57fa\u672c\u8bed\u6cd5<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ...\nFROM table_name\nORDER BY column1 &#91;ASC|DESC], column2 &#91;ASC|DESC], ...;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>column1, column2, \u2026<\/strong>\uff1a\u8981\u67e5\u8be2\u7684\u5217\u3002<\/li>\n\n\n\n<li><strong>table_name<\/strong>\uff1a\u6570\u636e\u8868\u7684\u540d\u79f0\u3002<\/li>\n\n\n\n<li><strong>ASC<\/strong>\uff1a\u6309\u5347\u5e8f\u6392\u5e8f\uff08\u9ed8\u8ba4\uff09\u3002<\/li>\n\n\n\n<li><strong>DESC<\/strong>\uff1a\u6309\u964d\u5e8f\u6392\u5e8f\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u591a\u4e2a\u5217\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5728 PHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>MySQLi<\/code> \u6216 <code>PDO<\/code> \u6765\u6267\u884c\u5e26\u6709 <code>ORDER BY<\/code> \u5b50\u53e5\u7684\u67e5\u8be2\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528 MySQLi \u6267\u884c ORDER BY<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u65b9\u5f0f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"my_database\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}\n\n\/\/ SQL \u67e5\u8be2\uff0c\u4f7f\u7528 ORDER BY \u5b50\u53e5\n$sql = \"SELECT id, username, email FROM users ORDER BY username ASC\";\n$result = $conn-&gt;query($sql);\n\n\/\/ \u68c0\u67e5\u7ed3\u679c\nif ($result-&gt;num_rows &gt; 0) {\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"ID: \" . $row&#91;\"id\"]. \" - \u7528\u6237\u540d: \" . $row&#91;\"username\"]. \" - \u90ae\u7bb1: \" . $row&#91;\"email\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"\u6ca1\u6709\u627e\u5230\u8bb0\u5f55\";\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u89e3\u6790<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>ORDER BY username ASC<\/code><\/strong>\uff1a\u6309 <code>username<\/code> \u5217\u7684\u5347\u5e8f\u6392\u5217\uff08\u9ed8\u8ba4\uff09\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <strong><code>DESC<\/code><\/strong> \u6765\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <strong><code>$result->fetch_assoc()<\/code><\/strong> \u6765\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u4ee5\u5173\u8054\u6570\u7ec4\u5f62\u5f0f\u8f93\u51fa\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u4f7f\u7528\u8fc7\u7a0b\u5316\u65b9\u5f0f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"my_database\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = mysqli_connect($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif (!$conn) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . mysqli_connect_error());\n}\n\n\/\/ \u6267\u884c\u67e5\u8be2\uff0c\u4f7f\u7528 ORDER BY \u5b50\u53e5\n$sql = \"SELECT id, username, email FROM users ORDER BY username DESC\";\n$result = mysqli_query($conn, $sql);\n\n\/\/ \u68c0\u67e5\u7ed3\u679c\nif (mysqli_num_rows($result) &gt; 0) {\n    while($row = mysqli_fetch_assoc($result)) {\n        echo \"ID: \" . $row&#91;\"id\"]. \" - \u7528\u6237\u540d: \" . $row&#91;\"username\"]. \" - \u90ae\u7bb1: \" . $row&#91;\"email\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"\u6ca1\u6709\u627e\u5230\u8bb0\u5f55\";\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\nmysqli_close($conn);\n?&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u89e3\u6790<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>ORDER BY username DESC<\/code><\/strong>\uff1a\u6309 <code>username<\/code> \u5217\u7684\u964d\u5e8f\u6392\u5217\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u4f7f\u7528 PDO \u6267\u884c ORDER BY<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u4f7f\u7528 PDO \u9884\u5904\u7406\u8bed\u53e5\u4e0e ORDER BY<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"my_database\";\n\ntry {\n    \/\/ \u521b\u5efa PDO \u8fde\u63a5\n    $pdo = new PDO(\"mysql:host=$servername;dbname=$dbname\", $username, $password);\n    \/\/ \u8bbe\u7f6e\u9519\u8bef\u6a21\u5f0f\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\n    \/\/ \u51c6\u5907 SQL \u67e5\u8be2\uff0c\u5e26\u6709 ORDER BY \u5b50\u53e5\n    $stmt = $pdo-&gt;prepare(\"SELECT id, username, email FROM users ORDER BY username ASC\");\n    $stmt-&gt;execute();\n\n    \/\/ \u68c0\u67e5\u7ed3\u679c\n    if ($stmt-&gt;rowCount() &gt; 0) {\n        \/\/ \u8f93\u51fa\u6bcf\u4e00\u884c\u6570\u636e\n        while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {\n            echo \"ID: \" . $row&#91;'id'] . \" - \u7528\u6237\u540d: \" . $row&#91;'username'] . \" - \u90ae\u7bb1: \" . $row&#91;'email'] . \"&lt;br&gt;\";\n        }\n    } else {\n        echo \"\u6ca1\u6709\u627e\u5230\u8bb0\u5f55\";\n    }\n} catch (PDOException $e) {\n    echo \"\u9519\u8bef: \" . $e-&gt;getMessage();\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$pdo = null;\n?&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u89e3\u6790<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <strong>\u9884\u5904\u7406\u8bed\u53e5<\/strong> \u548c <strong>\u53c2\u6570\u5316\u67e5\u8be2<\/strong> \u6765\u6267\u884c\u5e26\u6709 <code>ORDER BY<\/code> \u7684\u67e5\u8be2\uff0c\u9632\u6b62 SQL \u6ce8\u5165\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. ORDER BY \u5b50\u53e5\u7684\u5e38\u89c1\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u6309\u5355\u4e2a\u5217\u6392\u5e8f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email FROM users ORDER BY username ASC\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5347\u5e8f (<code>ASC<\/code>)<\/strong>\uff1a\u9ed8\u8ba4\u6392\u5e8f\u65b9\u5f0f\uff0c\u4ece\u5c0f\u5230\u5927\u3002<\/li>\n\n\n\n<li><strong>\u964d\u5e8f (<code>DESC<\/code>)<\/strong>\uff1a\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u6309\u591a\u4e2a\u5217\u6392\u5e8f<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u6309\u591a\u4e2a\u5217\u6765\u6392\u5e8f\uff0c\u591a\u4e2a\u5217\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email FROM users ORDER BY username ASC, email DESC\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5148\u6309 <code>username<\/code> \u5347\u5e8f\u6392\u5e8f\uff0c\u5982\u679c <code>username<\/code> \u76f8\u540c\uff0c\u5219\u6309 <code>email<\/code> \u964d\u5e8f\u6392\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.3 \u6392\u5e8f\u6570\u5b57\u5217\u4e0e\u5b57\u7b26\u4e32\u5217<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u5b57\u5217<\/strong>\uff1a\u6570\u5b57\u5217\u9ed8\u8ba4\u6309\u6570\u503c\u5927\u5c0f\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n\n\n\n<li><strong>\u5b57\u7b26\u4e32\u5217<\/strong>\uff1a\u5b57\u7b26\u4e32\u5217\u6309\u5b57\u6bcd\u987a\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.4 \u4f7f\u7528 <code>LIMIT<\/code> \u9650\u5236\u8fd4\u56de\u7ed3\u679c<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4e0e <code>ORDER BY<\/code> \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528 <code>LIMIT<\/code> \u6765\u9650\u5236\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email FROM users ORDER BY username ASC LIMIT 5\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>LIMIT<\/code><\/strong> \u7528\u6765\u9650\u5236\u67e5\u8be2\u7ed3\u679c\u7684\u6761\u6570\u3002\u4f8b\u5982\uff0c\u8fd4\u56de\u524d 5 \u6761\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u4f8b\u5b50\uff1aPHP MySQL ORDER BY \u7efc\u5408\u5e94\u7528<\/strong><\/h2>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>users<\/code> \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b <code>id<\/code>, <code>username<\/code>, <code>email<\/code>, \u548c <code>age<\/code> \u7b49\u5b57\u6bb5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1 \u6309 <code>age<\/code> \u6392\u5e8f\uff0c\u5e76\u663e\u793a\u524d 10 \u6761\u8bb0\u5f55<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email, age FROM users ORDER BY age DESC LIMIT 10\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>age DESC<\/code><\/strong>\uff1a\u6309 <code>age<\/code> \u5217\u964d\u5e8f\u6392\u5e8f\uff0c\u8fd4\u56de\u5e74\u9f84\u6700\u5927\u7684\u524d 10 \u4e2a\u7528\u6237\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2 \u6309\u591a\u4e2a\u5b57\u6bb5\u6392\u5e8f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email, age FROM users ORDER BY age ASC, username DESC\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5148\u6309 <strong><code>age<\/code><\/strong> \u5347\u5e8f\u6392\u5217\uff0c\u7136\u540e\u5bf9 <strong><code>username<\/code><\/strong> \u6309\u964d\u5e8f\u6392\u5217\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>ORDER BY<\/code><\/strong> \u662f SQL \u4e2d\u7528\u4e8e\u6392\u5e8f\u67e5\u8be2\u7ed3\u679c\u7684\u5173\u952e\u8bcd\uff0c\u652f\u6301\u6309\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u8fdb\u884c\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5e8f\u3002<\/li>\n\n\n\n<li>\u5728 PHP \u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 <strong>MySQLi<\/strong> \u6216 <strong>PDO<\/strong> \u6765\u6267\u884c\u5e26\u6709 <code>ORDER BY<\/code> \u5b50\u53e5\u7684\u67e5\u8be2\uff0c\u4fdd\u8bc1\u6392\u5e8f\u7684\u6570\u636e\u7b26\u5408\u9700\u6c42\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u4f7f\u7528 <code>LIMIT<\/code> \u6765\u9650\u5236\u67e5\u8be2\u7ed3\u679c\u7684\u6570\u91cf\uff0c\u548c <code>ORDER BY<\/code> \u7ed3\u5408\u4f7f\u7528\u65f6\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP \u4e2d\u4e0e MySQL \u4ea4\u4e92\u65f6\uff0cORDER BY \u5173\u952e\u8bcd\u7528\u4e8e\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u6392\u5e8f\u53ef\u4ee5\u662f\u6309\u5347\u5e8f\uff08ASC [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3112,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3111","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3111"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3111\/revisions"}],"predecessor-version":[{"id":3113,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3111\/revisions\/3113"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3112"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}