{"id":2637,"date":"2025-03-11T23:46:37","date_gmt":"2025-03-11T15:46:37","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2637"},"modified":"2025-03-11T23:46:37","modified_gmt":"2025-03-11T15:46:37","slug":"postgresql-order-by-%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/11\/postgresql-order-by-%e8%af%ad%e5%8f%a5\/","title":{"rendered":"PostgreSQL  ORDER BY\u00a0\u8bed\u53e5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c<code>ORDER BY<\/code> \u8bed\u53e5\u7528\u4e8e\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u4f60\u53ef\u4ee5\u6309\u7167\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u7684\u503c\u8fdb\u884c\u5347\u5e8f\uff08\u9ed8\u8ba4\uff09\u6216\u964d\u5e8f\u6392\u5e8f\u3002<code>ORDER BY<\/code> \u5b50\u53e5\u901a\u5e38\u4e0e <code>SELECT<\/code> \u67e5\u8be2\u4e00\u8d77\u4f7f\u7528\uff0c\u5e2e\u52a9\u4f60\u6309\u7279\u5b9a\u987a\u5e8f\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>ORDER BY<\/code> \u5b50\u53e5\u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ...\nFROM table_name\nWHERE condition\nORDER BY column1 &#91;ASC|DESC], column2 &#91;ASC|DESC], ...;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>column1<\/code>, <code>column2<\/code>, \u2026<\/strong>: \u8981\u6392\u5e8f\u7684\u5217\u540d\u3002<\/li>\n\n\n\n<li><strong><code>ASC<\/code><\/strong>: \u5347\u5e8f\uff08\u9ed8\u8ba4\uff09\uff0c\u4ece\u5c0f\u5230\u5927\u6392\u5217\u3002<\/li>\n\n\n\n<li><strong><code>DESC<\/code><\/strong>: \u964d\u5e8f\uff0c\u4ece\u5927\u5230\u5c0f\u6392\u5217\u3002<\/li>\n\n\n\n<li><strong><code>WHERE<\/code><\/strong>: \u53ef\u9009\uff0c\u7528\u4e8e\u6307\u5b9a\u8fc7\u6ee4\u6761\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>ORDER BY<\/code> \u7684\u57fa\u672c\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a <code>employees<\/code> \u7684\u8868\uff0c\u5305\u542b <code>name<\/code> \u548c <code>salary<\/code> \u4e24\u4e2a\u5b57\u6bb5\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528 <code>ORDER BY<\/code> \u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u540d\u5b57\u5347\u5e8f\u6392\u5e8f\nSELECT name FROM employees ORDER BY name ASC;\n\n-- \u6309\u85aa\u6c34\u964d\u5e8f\u6392\u5e8f\nSELECT name, salary FROM employees ORDER BY salary DESC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u6309\u591a\u4e2a\u5217\u6392\u5e8f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u6309\u4f18\u5148\u7ea7\u4f9d\u6b21\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5148\u6309 <code>salary<\/code> \u964d\u5e8f\u6392\u5e8f\uff0c\u518d\u6309 <code>name<\/code> \u5347\u5e8f\u6392\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5148\u6309\u85aa\u6c34\u964d\u5e8f\uff0c\u518d\u6309\u540d\u5b57\u5347\u5e8f\u6392\u5e8f\nSELECT name, salary FROM employees ORDER BY salary DESC, name ASC;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u6309 <code>salary<\/code> \u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\uff0c\u5982\u679c <code>salary<\/code> \u76f8\u540c\uff0c\u5219\u6309 <code>name<\/code> \u5347\u5e8f\u6392\u5e8f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong><code>ORDER BY<\/code> \u4e0e <code>NULL<\/code> \u503c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6392\u5e8f\u65f6\uff0c<code>NULL<\/code> \u503c\u7684\u6392\u5e8f\u884c\u4e3a\u662f\u6709\u7279\u5b9a\u89c4\u5219\u7684\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>NULL<\/code> \u503c\u4f1a\u88ab\u89c6\u4e3a\u6700\u5c0f\u7684\u503c\uff0c\u56e0\u6b64\u5728\u5347\u5e8f\u6392\u5e8f\u65f6\uff0c<code>NULL<\/code> \u4f1a\u6392\u5728\u6700\u524d\u9762\uff0c\u964d\u5e8f\u6392\u5e8f\u65f6\uff0c<code>NULL<\/code> \u4f1a\u6392\u5728\u6700\u540e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9ed8\u8ba4\u884c\u4e3a\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u540d\u5b57\u5347\u5e8f\u6392\u5e8f\uff0cNULL \u6392\u5728\u524d\u9762\nSELECT name FROM employees ORDER BY name ASC;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u901a\u8fc7 <code>NULLS FIRST<\/code> \u6216 <code>NULLS LAST<\/code> \u6765\u663e\u5f0f\u6307\u5b9a\u6392\u5e8f\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u540d\u5b57\u5347\u5e8f\u6392\u5e8f\uff0cNULL \u6392\u5728\u6700\u540e\nSELECT name FROM employees ORDER BY name ASC NULLS LAST;\n\n-- \u6309\u540d\u5b57\u964d\u5e8f\u6392\u5e8f\uff0cNULL \u6392\u5728\u6700\u524d\u9762\nSELECT name FROM employees ORDER BY name DESC NULLS FIRST;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong><code>ORDER BY<\/code> \u4e0e <code>LIMIT<\/code> \u4e00\u8d77\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ORDER BY<\/code> \u901a\u5e38\u4e0e <code>LIMIT<\/code> \u4e00\u8d77\u4f7f\u7528\uff0c\u5e2e\u52a9\u4f60\u83b7\u53d6\u7279\u5b9a\u6392\u5e8f\u7684\u524d\u51e0\u6761\u8bb0\u5f55\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u85aa\u6c34\u6700\u9ad8\u7684 3 \u540d\u5458\u5de5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u85aa\u6c34\u6700\u9ad8\u7684 3 \u540d\u5458\u5de5\nSELECT name, salary FROM employees ORDER BY salary DESC LIMIT 3;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong><code>ORDER BY<\/code> \u5728 <code>DISTINCT<\/code> \u67e5\u8be2\u4e2d\u7684\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ORDER BY<\/code> \u53ef\u4ee5\u4e0e <code>DISTINCT<\/code> \u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u8fd4\u56de\u4e0d\u540c\u7684\u7ed3\u679c\u5e76\u6392\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u4e0d\u540c\u7684\u85aa\u6c34\u503c\uff0c\u5e76\u6309\u964d\u5e8f\u6392\u5e8f\nSELECT DISTINCT salary FROM employees ORDER BY salary DESC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u4f7f\u7528\u8868\u8fbe\u5f0f\u8fdb\u884c\u6392\u5e8f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ORDER BY<\/code> \u4e5f\u53ef\u4ee5\u4f7f\u7528\u8868\u8fbe\u5f0f\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u6309\u7167\u85aa\u6c34\u52a0\u5956\u91d1\u603b\u548c\u6392\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u85aa\u6c34\u52a0\u5956\u91d1\u603b\u548c\u964d\u5e8f\u6392\u5e8f\nSELECT name, salary, bonus FROM employees\nORDER BY (salary + bonus) DESC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u4f7f\u7528\u522b\u540d\u8fdb\u884c\u6392\u5e8f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u67e5\u8be2\u4e2d\u5217\u7684\u522b\u540d\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u85aa\u6c34\u964d\u5e8f\u6392\u5e8f\u5e76\u4f7f\u7528\u522b\u540d\nSELECT name, salary AS employee_salary FROM employees\nORDER BY employee_salary DESC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>\u6027\u80fd\u8003\u8651<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ORDER BY<\/code> \u5b50\u53e5\u9700\u8981\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u5e8f\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u67e5\u8be2\u6027\u80fd\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4f60\u5728\u6392\u5e8f\u7684\u5217\u4e0a\u6ca1\u6709\u7d22\u5f15\uff0c\u6392\u5e8f\u64cd\u4f5c\u53ef\u80fd\u4f1a\u6bd4\u8f83\u6162\u3002\u521b\u5efa\u7d22\u5f15\u6709\u52a9\u4e8e\u63d0\u9ad8\u6392\u5e8f\u64cd\u4f5c\u7684\u6548\u7387\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ORDER BY<\/code> \u7528\u4e8e\u6307\u5b9a\u67e5\u8be2\u7ed3\u679c\u7684\u6392\u5e8f\u987a\u5e8f\uff0c\u652f\u6301\u5347\u5e8f\uff08<code>ASC<\/code>\uff09\u548c\u964d\u5e8f\uff08<code>DESC<\/code>\uff09\u6392\u5e8f\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u6309\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u6307\u5b9a\u6bcf\u4e2a\u5217\u7684\u6392\u5e8f\u987a\u5e8f\u3002<\/li>\n\n\n\n<li>\u6392\u5e8f\u65f6\uff0c<code>NULL<\/code> \u503c\u7684\u9ed8\u8ba4\u6392\u5e8f\u4f4d\u7f6e\u662f\u6700\u524d\uff08\u5347\u5e8f\uff09\u6216\u6700\u540e\uff08\u964d\u5e8f\uff09\uff0c\u4f60\u4e5f\u53ef\u4ee5\u663e\u5f0f\u6307\u5b9a <code>NULLS FIRST<\/code> \u6216 <code>NULLS LAST<\/code>\u3002<\/li>\n\n\n\n<li><code>ORDER BY<\/code> \u53ef\u4ee5\u4e0e <code>LIMIT<\/code> \u548c <code>OFFSET<\/code> \u7ed3\u5408\u4f7f\u7528\uff0c\u7528\u4e8e\u5206\u9875\u67e5\u8be2\u3002<\/li>\n\n\n\n<li>\u6027\u80fd\u4e0a\uff0c<code>ORDER BY<\/code> \u53ef\u80fd\u4f1a\u5f71\u54cd\u67e5\u8be2\u901f\u5ea6\uff0c\u7279\u522b\u662f\u5728\u5927\u6570\u636e\u96c6\u548c\u6ca1\u6709\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u638c\u63e1 <code>ORDER BY<\/code> \u8bed\u53e5\uff0c\u4f60\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff0c\u6ee1\u8db3\u5404\u79cd\u590d\u6742\u7684\u67e5\u8be2\u9700\u6c42\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PostgreSQL \u4e2d\uff0cORDER BY \u8bed\u53e5\u7528\u4e8e\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u4f60\u53ef\u4ee5\u6309\u7167\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u7684\u503c\u8fdb\u884c\u5347\u5e8f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2638,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2637","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=2637"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2637\/revisions"}],"predecessor-version":[{"id":2639,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2637\/revisions\/2639"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2638"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}