{"id":5525,"date":"2024-11-14T09:13:06","date_gmt":"2024-11-14T01:13:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5525\/"},"modified":"2024-11-14T09:13:06","modified_gmt":"2024-11-14T01:13:06","slug":"mysql%e5%88%86%e7%bb%84%e6%8e%92%e5%ba%8f%e6%80%8e%e4%b9%88%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5525\/","title":{"rendered":"mysql\u5206\u7ec4\u6392\u5e8f\u600e\u4e48\u67e5"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 group by \u548c order by \u5b50\u53e5\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u548c\u6392\u5e8f\u3002\u9996\u5148\uff0c\u4f7f\u7528 group by \u5b50\u53e5\u6309\u6307\u5b9a\u5217\u6216\u8868\u8fbe\u5f0f\u5206\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 order by \u5b50\u53e5\u6309\u6307\u5b9a\u5217\u6216\u8868\u8fbe\u5f0f\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u5e8f\u3002\u901a\u8fc7\u4f7f\u7528 group by \u548c order by \u5b50\u53e5\uff0c\u53ef\u4ee5\u6309\u6307\u5b9a\u5217\u6216\u8868\u8fbe\u5f0f\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u5e76\u6392\u5e8f\uff0c\u4f8b\u5982\u6309\u67d0\u4e2a\u4ea7\u54c1 id \u5206\u7ec4\u5e76\u6309\u9500\u552e\u6570\u91cf\u6392\u5e8f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052907512453431.jpg\" class=\"aligncenter\" title=\"mysql\u5206\u7ec4\u6392\u5e8f\u600e\u4e48\u67e5\u63d2\u56fe\" alt=\"mysql\u5206\u7ec4\u6392\u5e8f\u600e\u4e48\u67e5\u63d2\u56fe\" \/><\/p>\n<h2>MySQL \u5206\u7ec4\u6392\u5e8f\u67e5\u8be2\u65b9\u6cd5<\/h2>\n<p>\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 GROUP BY \u548c ORDER BY \u5b50\u53e5\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u548c\u6392\u5e8f\u3002<\/p>\n<h3>\u5206\u7ec4<\/h3>\n<p>GROUP BY \u5b50\u53e5\u5c06\u884c\u6309\u6307\u5b9a\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u5206\u7ec4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u7ec4\u7684\u7ed3\u679c\u805a\u5408\u5728\u4e00\u8d77\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT \u805a\u5408\u51fd\u6570(\u5217\u6216\u8868\u8fbe\u5f0f)\nFROM \u8868\u540d\nGROUP BY \u5217\u6216\u8868\u8fbe\u5f0f<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u6392\u5e8f<\/h3>\n<p>ORDER BY \u5b50\u53e5\u5bf9\u7ed3\u679c\u96c6\u6309\u6307\u5b9a\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u8fdb\u884c\u6392\u5e8f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT \u5217\u6216\u8868\u8fbe\u5f0f\nFROM \u8868\u540d\nORDER BY \u5217\u6216\u8868\u8fbe\u5f0f [ASC|DESC]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u5206\u7ec4\u6392\u5e8f<\/h3>\n<p>\u8981\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>SELECT \u805a\u5408\u51fd\u6570(\u5217\u6216\u8868\u8fbe\u5f0f)\nFROM \u8868\u540d\nGROUP BY \u5217\u6216\u8868\u8fbe\u5f0f\nORDER BY \u805a\u5408\u51fd\u6570(\u5217\u6216\u8868\u8fbe\u5f0f) [ASC|DESC]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a sales \u7684\u8868\u683c\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>product_id<\/th>\n<th>product_name<\/th>\n<th>quantity<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>Product A<\/td>\n<td>10<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Product B<\/td>\n<td>15<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>Product A<\/td>\n<td>15<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Product B<\/td>\n<td>20<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u4e0b\u67e5\u8be2\u5c06\u6309 product_id \u5206\u7ec4\u5e76\u6309\u9500\u552e\u6570\u91cf\u4ece\u9ad8\u5230\u4f4e\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre>SELECT SUM(quantity) AS total_quantity\nFROM sales\nGROUP BY product_id\nORDER BY total_quantity DESC;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>product_id<\/th>\n<th>total_quantity<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>2<\/td>\n<td>35<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>25<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5206\u7ec4\u6392\u5e8f\u600e\u4e48\u67e5\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\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 group by \u548c order by \u5b50\u53e5\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u548c\u6392\u5e8f\u3002\u9996\u5148\uff0c\u4f7f\u7528 group by \u5b50\u53e5\u6309\u6307\u5b9a\u5217\u6216\u8868\u8fbe\u5f0f\u5206\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 order by \u5b50\u53e5\u6309\u6307\u5b9a\u5217\u6216\u8868\u8fbe\u5f0f\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u5e8f\u3002\u901a\u8fc7\u4f7f\u7528 group by \u548c order by \u5b50\u53e5\uff0c\u53ef\u4ee5\u6309\u6307\u5b9a\u5217\u6216\u8868\u8fbe\u5f0f\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u5e76\u6392\u5e8f\uff0c\u4f8b\u5982\u6309\u67d0\u4e2a\u4ea7\u54c1 id \u5206\u7ec4\u5e76\u6309\u9500\u552e\u6570\u91cf\u6392\u5e8f\u3002 MySQL \u5206\u7ec4\u6392\u5e8f\u67e5\u8be2\u65b9\u6cd5 \u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 GROUP BY \u548c ORDER BY \u5b50\u53e5\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u548c\u6392\u5e8f\u3002 \u5206\u7ec4 GROUP BY \u5b50\u53e5\u5c06\u884c\u6309\u6307\u5b9a\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u5206\u7ec4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u7ec4\u7684\u7ed3\u679c\u805a\u5408\u5728\u4e00\u8d77\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT \u805a\u5408\u51fd\u6570(\u5217\u6216\u8868\u8fbe\u5f0f) FROM \u8868\u540d GROUP BY \u5217\u6216\u8868\u8fbe\u5f0f \u767b\u5f55\u540e\u590d\u5236 \u6392\u5e8f ORDER BY \u5b50\u53e5\u5bf9\u7ed3\u679c\u96c6\u6309\u6307\u5b9a\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u8fdb\u884c\u6392\u5e8f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT \u5217\u6216\u8868\u8fbe\u5f0f FROM \u8868\u540d ORDER [&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-5525","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5525","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=5525"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5525\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}