{"id":5450,"date":"2024-11-14T13:26:34","date_gmt":"2024-11-14T05:26:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5450\/"},"modified":"2024-11-14T13:26:34","modified_gmt":"2024-11-14T05:26:34","slug":"mysql%e6%8e%92%e5%ba%8f%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5450\/","title":{"rendered":"mysql\u6392\u5e8f\u600e\u4e48\u8bbe\u7f6e"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\uff0c\u901a\u8fc7 order by \u5b50\u53e5\u8fdb\u884c\u6392\u5e8f\uff0c\u6839\u636e\u6307\u5b9a\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5347\u5e8f\uff08asc\uff09\u6216\u964d\u5e8f\uff08desc\uff09\u6392\u5217\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8868\u8fbe\u5f0f\u81ea\u5b9a\u4e49\u6392\u5e8f\u3001\u6307\u5b9a\u591a\u5217\u6392\u5e8f\u548c\u66f4\u6539 null \u503c\u7684\u5904\u7406\u65b9\u5f0f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052814062612201.jpg\" class=\"aligncenter\" title=\"mysql\u6392\u5e8f\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" alt=\"mysql\u6392\u5e8f\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u6392\u5e8f\u8bbe\u7f6e<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u6392\u5e8f\u53ef\u4ee5\u901a\u8fc7 ORDER BY \u5b50\u53e5\u6765\u5b9e\u73b0\u3002ORDER BY \u5b50\u53e5\u5141\u8bb8\u4f60\u6839\u636e\u6307\u5b9a\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT column_name(s)\nFROM table_name\nORDER BY column_name(s) [ASC | DESC]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>column_name(s)\uff1a\u8981\u6392\u5e8f\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u3002<\/li>\n<li>ASC\uff1a\u5347\u5e8f\u6392\u5e8f\uff08\u4ece\u5c0f\u5230\u5927\uff09\u3002<\/li>\n<li>DESC\uff1a\u964d\u5e8f\u6392\u5e8f\uff08\u4ece\u5927\u5230\u5c0f\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><strong>\u6839\u636e\u5458\u5de5\u59d3\u540d\u5347\u5e8f\u6392\u5e8f\uff1a<\/strong><\/p>\n<pre>SELECT name\nFROM employees\nORDER BY name ASC;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6839\u636e\u9500\u552e\u989d\u964d\u5e8f\u6392\u5e8f\uff1a<\/strong><\/p>\n<pre>SELECT product_name, sales\nFROM products\nORDER BY sales DESC;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f\uff1a<\/strong><\/p>\n<p>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528\u5217\u540d\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8868\u8fbe\u5f0f\u6765\u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 CASE \u8bed\u53e5\u57fa\u4e8e\u7279\u5b9a\u6761\u4ef6\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre>SELECT name\nFROM employees\nORDER BY CASE\n  WHEN gender = 'Male' THEN 1\n  WHEN gender = 'Female' THEN 2\n  ELSE 3\nEND;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5458\u5de5\u6309\u6027\u522b\u6392\u5e8f\uff0c\u7537\u6027\u4f18\u5148\u4e8e\u5973\u6027\u3002<\/p>\n<p><strong>\u591a\u5217\u6392\u5e8f\uff1a<\/strong><\/p>\n<p>ORDER BY \u5b50\u53e5\u53ef\u4ee5\u540c\u65f6\u6307\u5b9a\u591a\u4e2a\u5217\u7528\u4e8e\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u6309\u59d3\u6c0f\u5347\u5e8f\u6392\u5e8f\uff0c\u7136\u540e\u6309\u540d\u5b57\u5347\u5e8f\u6392\u5e8f\uff1a<\/p>\n<pre>SELECT name\nFROM employees\nORDER BY last_name ASC, first_name ASC;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>NULL \u503c\u5904\u7406\uff1a<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cNULL \u503c\u5728\u6392\u5e8f\u4e2d\u88ab\u89c6\u4e3a\u6700\u5927\u503c\u3002\u8981\u66f4\u6539\u6b64\u884c\u4e3a\uff0c\u53ef\u4ee5\u4f7f\u7528 NULLS FIRST \u6216 NULLS LAST \u5173\u952e\u5b57\uff1a<\/p>\n<ul>\n<li>NULLS FIRST\uff1a\u5c06 NULL \u503c\u653e\u5728\u6392\u5e8f\u7ed3\u679c\u7684\u5f00\u5934\u3002<\/li>\n<li>NULLS LAST\uff1a\u5c06 NULL \u503c\u653e\u5728\u6392\u5e8f\u7ed3\u679c\u7684\u672b\u5c3e\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u6309\u9500\u552e\u989d\u964d\u5e8f\u6392\u5e8f\uff0c\u5e76\u5c06 NULL \u503c\u653e\u5728\u5f00\u5934\uff1a<\/p>\n<pre>SELECT product_name, sales\nFROM products\nORDER BY sales DESC NULLS FIRST;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6392\u5e8f\u600e\u4e48\u8bbe\u7f6e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 mysql \u4e2d\uff0c\u901a\u8fc7 order by \u5b50\u53e5\u8fdb\u884c\u6392\u5e8f\uff0c\u6839\u636e\u6307\u5b9a\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5347\u5e8f\uff08asc\uff09\u6216\u964d\u5e8f\uff08desc\uff09\u6392\u5217\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8868\u8fbe\u5f0f\u81ea\u5b9a\u4e49\u6392\u5e8f\u3001\u6307\u5b9a\u591a\u5217\u6392\u5e8f\u548c\u66f4\u6539 null \u503c\u7684\u5904\u7406\u65b9\u5f0f\u3002 MySQL \u6392\u5e8f\u8bbe\u7f6e \u5728 MySQL \u4e2d\uff0c\u6392\u5e8f\u53ef\u4ee5\u901a\u8fc7 ORDER BY \u5b50\u53e5\u6765\u5b9e\u73b0\u3002ORDER BY \u5b50\u53e5\u5141\u8bb8\u4f60\u6839\u636e\u6307\u5b9a\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u5e8f\u3002 \u8bed\u6cd5\uff1a SELECT column_name(s) FROM table_name ORDER BY column_name(s) [ASC | DESC] \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a column_name(s)\uff1a\u8981\u6392\u5e8f\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u3002 ASC\uff1a\u5347\u5e8f\u6392\u5e8f\uff08\u4ece\u5c0f\u5230\u5927\uff09\u3002 DESC\uff1a\u964d\u5e8f\u6392\u5e8f\uff08\u4ece\u5927\u5230\u5c0f\uff09\u3002 \u793a\u4f8b\uff1a \u6839\u636e\u5458\u5de5\u59d3\u540d\u5347\u5e8f\u6392\u5e8f\uff1a SELECT name FROM employees ORDER BY name ASC; \u767b\u5f55\u540e\u590d\u5236 \u6839\u636e\u9500\u552e\u989d\u964d\u5e8f\u6392\u5e8f\uff1a SELECT product_name, sales FROM products ORDER BY sales DESC; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5450","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5450"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5450\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}