{"id":5694,"date":"2024-11-14T13:55:15","date_gmt":"2024-11-14T05:55:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5694\/"},"modified":"2024-11-14T13:55:15","modified_gmt":"2024-11-14T05:55:15","slug":"sql%e7%9a%84max%e5%87%bd%e6%95%b0%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5694\/","title":{"rendered":"sql\u7684max\u51fd\u6570\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  max \u51fd\u6570\u8fd4\u56de\u4e00\u7ec4\u503c\u4e2d\u7684\u6700\u5927\u503c\uff0c\u5176\u8bed\u6cd5\u4e3a max(expression)\uff0cexpression \u6307\u8981\u8ba1\u7b97\u6700\u5927\u503c\u7684\u8868\u8fbe\u5f0f\u3002\u5b83\u53ef\u7528\u4e8e\u5355\u884c\u67e5\u8be2\u3001\u5206\u7ec4\u8fd0\u7b97\u548c\u5b50\u67e5\u8be2\u4e2d\u3002\u5bf9\u4e8e\u5305\u542b\u6587\u672c\u503c\u6216\u7a7a\u503c\u7684\u8868\u8fbe\u5f0f\uff0cmax \u51fd\u6570\u5206\u522b\u8fd4\u56de\u6587\u672c\u503c\u7684\u6700\u5927\u503c\u548c null\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053020511734578.jpg\" class=\"aligncenter\" title=\"sql\u7684max\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u7684max\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL MAX \u51fd\u6570\u7528\u6cd5<\/strong><\/p>\n<p>MAX \u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u4e00\u7ec4\u503c\u4e2d\u7684\u6700\u5927\u503c\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>MAX(expression)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<ul>\n<li> <strong>expression\uff1a<\/strong>\u8981\u8ba1\u7b97\u6700\u5927\u503c\u7684\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u662f\u5217\u540d\u3001\u5e38\u91cf\u6216\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<pre>-- \u67e5\u627e\u8868 \"employees\" \u4e2d\u6700\u9ad8\u85aa\u6c34\nSELECT MAX(salary) FROM employees;\n\n-- \u67e5\u627e\u8fc7\u53bb 30 \u5929\u5185\u8ba2\u5355\u603b\u989d\u7684\u6700\u5927\u503c\nSELECT MAX(total_amount) FROM orders WHERE order_date &gt; DATE('now', '-30 days');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>MAX \u51fd\u6570\u5ffd\u7565\u7a7a\u503c\u3002<\/li>\n<li>\u5982\u679c\u8868\u8fbe\u5f0f\u5305\u542b\u6587\u672c\u503c\uff0c\u5219 MAX \u51fd\u6570\u5c06\u8fd4\u56de\u6587\u672c\u503c\u7684\u6700\u5927\u503c\uff0c\u800c\u4e0d\u662f\u6570\u503c\u7684\u6700\u5927\u503c\u3002<\/li>\n<li>MAX \u51fd\u6570\u53ea\u80fd\u5e94\u7528\u4e8e\u540c\u4e00\u6570\u636e\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u3002<\/li>\n<li>\u5982\u679c expression \u4e3a\u7a7a\u96c6\uff0c\u5219 MAX \u51fd\u6570\u8fd4\u56de NULL\u3002<\/li>\n<\/ul>\n<p><strong>\u62d3\u5c55\u5e94\u7528<\/strong><\/p>\n<p><strong>\u5206\u7ec4\u8fd0\u7b97\u4e2d\u7684 MAX \u51fd\u6570\uff1a<\/strong><\/p>\n<ul>\n<li>MAX \u51fd\u6570\u53ef\u7528\u4e8e\u5206\u7ec4\u8fd0\u7b97\u4e2d\uff0c\u627e\u5230\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u6700\u5927\u503c\u3002<\/li>\n<li>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u67e5\u627e\u6bcf\u4e2a\u90e8\u95e8\u7684\u6700\u9ad8\u85aa\u6c34\uff1a<\/li>\n<\/ul>\n<pre>SELECT department_id, MAX(salary) AS max_salary\nFROM employees\nGROUP BY department_id;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5b50\u67e5\u8be2\u4e2d\u7684 MAX \u51fd\u6570\uff1a<\/strong><\/p>\n<ul>\n<li>MAX \u51fd\u6570\u53ef\u7528\u4e8e\u5b50\u67e5\u8be2\u4e2d\uff0c\u4f8b\u5982\u67e5\u627e\u5d4c\u5957\u67e5\u8be2\u8fd4\u56de\u7684\u6700\u5927\u503c\u3002<\/li>\n<li>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u67e5\u627e\u6240\u6709\u5177\u6709\u6700\u5927\u8ba2\u5355\u603b\u989d\u7684\u8ba2\u5355\u7f16\u53f7\uff1a<\/li>\n<\/ul>\n<pre>SELECT order_id\nFROM orders\nWHERE total_amount = (SELECT MAX(total_amount) FROM orders);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u7684max\u51fd\u6570\u600e\u4e48\u7528\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>max \u51fd\u6570\u8fd4\u56de\u4e00\u7ec4\u503c\u4e2d\u7684\u6700\u5927\u503c\uff0c\u5176\u8bed\u6cd5\u4e3a max(expression)\uff0cexpression \u6307\u8981\u8ba1\u7b97\u6700\u5927\u503c\u7684\u8868\u8fbe\u5f0f\u3002\u5b83\u53ef\u7528\u4e8e\u5355\u884c\u67e5\u8be2\u3001\u5206\u7ec4\u8fd0\u7b97\u548c\u5b50\u67e5\u8be2\u4e2d\u3002\u5bf9\u4e8e\u5305\u542b\u6587\u672c\u503c\u6216\u7a7a\u503c\u7684\u8868\u8fbe\u5f0f\uff0cmax \u51fd\u6570\u5206\u522b\u8fd4\u56de\u6587\u672c\u503c\u7684\u6700\u5927\u503c\u548c null\u3002 SQL MAX \u51fd\u6570\u7528\u6cd5 MAX \u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u4e00\u7ec4\u503c\u4e2d\u7684\u6700\u5927\u503c\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a MAX(expression) \u767b\u5f55\u540e\u590d\u5236 \u7528\u6cd5 expression\uff1a\u8981\u8ba1\u7b97\u6700\u5927\u503c\u7684\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u662f\u5217\u540d\u3001\u5e38\u91cf\u6216\u8868\u8fbe\u5f0f\u3002 \u793a\u4f8b &#8212; \u67e5\u627e\u8868 &#8220;employees&#8221; \u4e2d\u6700\u9ad8\u85aa\u6c34 SELECT MAX(salary) FROM employees; &#8212; \u67e5\u627e\u8fc7\u53bb 30 \u5929\u5185\u8ba2\u5355\u603b\u989d\u7684\u6700\u5927\u503c SELECT MAX(total_amount) FROM orders WHERE order_date &gt; DATE(&#8216;now&#8217;, &#8216;-30 days&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\u4e8b\u9879 MAX \u51fd\u6570\u5ffd\u7565\u7a7a\u503c\u3002 \u5982\u679c\u8868\u8fbe\u5f0f\u5305\u542b\u6587\u672c\u503c\uff0c\u5219 MAX \u51fd\u6570\u5c06\u8fd4\u56de\u6587\u672c\u503c\u7684\u6700\u5927\u503c\uff0c\u800c\u4e0d\u662f\u6570\u503c\u7684\u6700\u5927\u503c\u3002 MAX \u51fd\u6570\u53ea\u80fd\u5e94\u7528\u4e8e\u540c\u4e00\u6570\u636e\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u3002 \u5982\u679c expression \u4e3a\u7a7a\u96c6\uff0c\u5219 MAX \u51fd\u6570\u8fd4\u56de NULL\u3002 \u62d3\u5c55\u5e94\u7528 \u5206\u7ec4\u8fd0\u7b97\u4e2d\u7684 [&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-5694","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5694","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=5694"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5694\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}