{"id":445,"date":"2024-12-22T13:08:23","date_gmt":"2024-12-22T05:08:23","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=445"},"modified":"2024-12-22T13:08:23","modified_gmt":"2024-12-22T05:08:23","slug":"sql-%e7%9a%84%e6%b1%87%e6%80%bb%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/22\/sql-%e7%9a%84%e6%b1%87%e6%80%bb%e5%88%86%e6%9e%90\/","title":{"rendered":"SQL \u7684\u6c47\u603b\u5206\u6790"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">SQL \u7684\u6c47\u603b\u5206\u6790\u529f\u80fd\u5f3a\u5927\uff0c\u4e3b\u8981\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u7edf\u8ba1\u3001\u5206\u7ec4\u3001\u805a\u5408\u548c\u7b5b\u9009\u3002\u6c47\u603b\u5206\u6790\u5728\u62a5\u8868\u3001\u6570\u636e\u6316\u6398\u3001\u5546\u4e1a\u667a\u80fd\u4e2d\u975e\u5e38\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u6d77\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u5f97\u51fa\u5173\u952e\u6307\u6807\u548c\u8d8b\u52bf\u3002\u4ee5\u4e0b\u662f SQL \u6c47\u603b\u5206\u6790\u7684\u8be6\u7ec6\u8bb2\u89e3\uff0c\u7ed3\u5408\u6700\u65b0 SQL \u529f\u80fd\u4e0e\u5b9e\u8df5\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>\u805a\u5408\u51fd\u6570<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SQL \u63d0\u4f9b\u4e86\u591a\u79cd\u805a\u5408\u51fd\u6570\uff0c\u7528\u4e8e\u6c47\u603b\u5206\u6790\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u7528\u805a\u5408\u51fd\u6570<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>SUM()<\/code><\/td><td>\u8ba1\u7b97\u67d0\u5217\u6570\u503c\u7684\u603b\u548c<\/td><\/tr><tr><td><code>AVG()<\/code><\/td><td>\u8ba1\u7b97\u67d0\u5217\u6570\u503c\u7684\u5e73\u5747\u503c<\/td><\/tr><tr><td><code>COUNT()<\/code><\/td><td>\u7edf\u8ba1\u884c\u6570\u6216\u67d0\u5217\u975e NULL \u503c\u7684\u6570\u91cf<\/td><\/tr><tr><td><code>MAX()<\/code><\/td><td>\u8fd4\u56de\u67d0\u5217\u7684\u6700\u5927\u503c<\/td><\/tr><tr><td><code>MIN()<\/code><\/td><td>\u8fd4\u56de\u67d0\u5217\u7684\u6700\u5c0f\u503c<\/td><\/tr><tr><td><code>GROUP_CONCAT()<\/code><\/td><td>\u5c06\u5206\u7ec4\u540e\u7684\u503c\u62fc\u63a5\u6210\u5b57\u7b26\u4e32\uff08MySQL\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u805a\u5408\u8ba1\u7b97<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u5f20 <code>sales<\/code> \u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id   | product   | category   | quantity | price\n-------------------------------------------------\n1    | Laptop    | Electronics| 2        | 1000\n2    | Smartphone| Electronics| 3        | 500\n3    | Chair     | Furniture  | 5        | 200\n4    | Table     | Furniture  | 2        | 800\n5    | Laptop    | Electronics| 1        | 1000<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8ba1\u7b97\u603b\u9500\u552e\u989d<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT SUM(quantity * price) AS total_sales FROM sales;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>total_sales\n-----------\n5900<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8ba1\u7b97\u6bcf\u4e2a\u7c7b\u522b\u7684\u5e73\u5747\u4ef7\u683c<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    category,\n    AVG(price) AS avg_price\nFROM \n    sales\nGROUP BY \n    category;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>category     | avg_price\n-------------------------\nElectronics  | 833.33\nFurniture    | 500<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u7edf\u8ba1\u6bcf\u4e2a\u4ea7\u54c1\u7684\u9500\u552e\u603b\u91cf<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    product,\n    SUM(quantity) AS total_quantity\nFROM \n    sales\nGROUP BY \n    product;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>product       | total_quantity\n------------------------------\nLaptop        | 3\nSmartphone    | 3\nChair         | 5\nTable         | 2<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>\u5206\u7ec4\u5206\u6790\uff08GROUP BY\uff09<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u662f\u6c47\u603b\u5206\u6790\u7684\u6838\u5fc3\uff0c\u7528\u4e8e\u5c06\u6570\u636e\u6309\u67d0\u5217\uff08\u6216\u591a\u5217\uff09\u5206\u7ec4\uff0c\u7136\u540e\u8fdb\u884c\u805a\u5408\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u591a\u5b57\u6bb5\u5206\u7ec4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5206\u7ec4\u53ef\u4ee5\u57fa\u4e8e\u591a\u4e2a\u5b57\u6bb5\uff0c\u4ee5\u5b9e\u73b0\u66f4\u7ec6\u7c92\u5ea6\u7684\u7edf\u8ba1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u6309\u7c7b\u522b\u548c\u4ea7\u54c1\u5206\u7ec4\uff0c\u7edf\u8ba1\u9500\u552e\u6570\u91cf<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    category,\n    product,\n    SUM(quantity) AS total_quantity\nFROM \n    sales\nGROUP BY \n    category, product;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>category     | product       | total_quantity\n----------------------------------------------\nElectronics  | Laptop        | 3\nElectronics  | Smartphone    | 3\nFurniture    | Chair         | 5\nFurniture    | Table         | 2<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>\u6761\u4ef6\u805a\u5408\uff08HAVING\uff09<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u7528\u4e8e\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\u3002\u4e0e <code>WHERE<\/code> \u4e0d\u540c\uff0c<code>HAVING<\/code> \u662f\u5728\u5206\u7ec4\u548c\u805a\u5408\u4e4b\u540e\u8d77\u4f5c\u7528\u7684\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u7b5b\u9009\u603b\u9500\u552e\u989d\u5927\u4e8e $1000 \u7684\u7c7b\u522b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    category,\n    SUM(quantity * price) AS total_sales\nFROM \n    sales\nGROUP BY \n    category\nHAVING \n    total_sales &gt; 1000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>category     | total_sales\n---------------------------\nElectronics  | 4000\nFurniture    | 1900<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u7a97\u53e3\u51fd\u6570<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a97\u53e3\u51fd\u6570\uff08Window Functions\uff09\u5728 SQL \u7684\u6c47\u603b\u5206\u6790\u4e2d\u975e\u5e38\u5f3a\u5927\uff0c\u5b83\u5141\u8bb8\u5728\u4e0d\u6539\u53d8\u884c\u6570\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u7edf\u8ba1\u548c\u6392\u540d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u7528\u7a97\u53e3\u51fd\u6570<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>ROW_NUMBER()<\/code><\/td><td>\u6309\u6307\u5b9a\u6392\u5e8f\u5206\u914d\u8fde\u7eed\u884c\u53f7<\/td><\/tr><tr><td><code>RANK()<\/code><\/td><td>\u6309\u6307\u5b9a\u6392\u5e8f\u5206\u914d\u6392\u540d\uff0c\u76f8\u540c\u6392\u540d\u8df3\u8fc7\u540d\u6b21<\/td><\/tr><tr><td><code>DENSE_RANK()<\/code><\/td><td>\u7c7b\u4f3c <code>RANK()<\/code>\uff0c\u4f46\u4e0d\u4f1a\u8df3\u8fc7\u540d\u6b21<\/td><\/tr><tr><td><code>SUM()<\/code><\/td><td>\u5728\u7a97\u53e3\u8303\u56f4\u5185\u8ba1\u7b97\u603b\u548c<\/td><\/tr><tr><td><code>AVG()<\/code><\/td><td>\u5728\u7a97\u53e3\u8303\u56f4\u5185\u8ba1\u7b97\u5e73\u5747\u503c<\/td><\/tr><tr><td><code>COUNT()<\/code><\/td><td>\u5728\u7a97\u53e3\u8303\u56f4\u5185\u7edf\u8ba1\u884c\u6570<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u6309\u7c7b\u522b\u5bf9\u4ea7\u54c1\u8fdb\u884c\u9500\u91cf\u6392\u540d<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    category,\n    product,\n    SUM(quantity) AS total_quantity,\n    RANK() OVER (PARTITION BY category ORDER BY SUM(quantity) DESC) AS rank\nFROM \n    sales\nGROUP BY \n    category, product;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>category     | product       | total_quantity | rank\n-----------------------------------------------------\nElectronics  | Laptop        | 3              | 1\nElectronics  | Smartphone    | 3              | 1\nFurniture    | Chair         | 5              | 1\nFurniture    | Table         | 2              | 2<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>\u5b50\u67e5\u8be2\u4e0e\u5d4c\u5957\u5206\u6790<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u67e5\u8be2\u53ef\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6c47\u603b\u9700\u6c42\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u67e5\u8be2\u9500\u91cf\u6700\u9ad8\u7684\u4ea7\u54c1\u7c7b\u522b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    category\nFROM \n    (SELECT \n         category, \n         SUM(quantity) AS total_quantity\n     FROM \n         sales\n     GROUP BY \n         category) AS subquery\nWHERE \n    total_quantity = (SELECT MAX(total_quantity) \n                      FROM \n                          (SELECT SUM(quantity) AS total_quantity \n                           FROM sales \n                           GROUP BY category) AS max_query);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>category\n---------\nFurniture<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong>CTE\uff08\u516c\u5171\u8868\u8868\u8fbe\u5f0f\uff09<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CTE \u662f\u63d0\u9ad8\u67e5\u8be2\u53ef\u8bfb\u6027\u7684\u91cd\u8981\u5de5\u5177\uff0c\u9002\u5408\u5206\u6b65\u5904\u7406\u590d\u6742\u7684\u6c47\u603b\u5206\u6790\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u5206\u6b65\u8ba1\u7b97\u6bcf\u4e2a\u7c7b\u522b\u7684\u603b\u9500\u552e\u989d\uff0c\u518d\u7b5b\u9009\u51fa\u9500\u91cf\u6700\u9ad8\u7684\u7c7b\u522b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>WITH CategorySales AS (\n    SELECT \n        category,\n        SUM(quantity * price) AS total_sales\n    FROM \n        sales\n    GROUP BY \n        category\n)\nSELECT \n    category\nFROM \n    CategorySales\nWHERE \n    total_sales = (SELECT MAX(total_sales) FROM CategorySales);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>JSON \u6c47\u603b\u5206\u6790<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL 8.0+ \u548c PostgreSQL \u63d0\u4f9b\u4e86\u5bf9 JSON \u6570\u636e\u7684\u652f\u6301\uff0c\u9002\u7528\u4e8e\u975e\u7ed3\u6784\u5316\u6570\u636e\u7684\u6c47\u603b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4ee5 JSON \u683c\u5f0f\u8fd4\u56de\u6bcf\u4e2a\u7c7b\u522b\u7684\u4ea7\u54c1\u548c\u603b\u9500\u91cf<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    category,\n    JSON_ARRAYAGG(\n        JSON_OBJECT('product', product, 'total_quantity', SUM(quantity))\n    ) AS product_summary\nFROM \n    sales\nGROUP BY \n    category;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>category     | product_summary\n----------------------------------------\nElectronics  | &#91;{\"product\": \"Laptop\", \"total_quantity\": 3}, \n                {\"product\": \"Smartphone\", \"total_quantity\": 3}]\nFurniture    | &#91;{\"product\": \"Chair\", \"total_quantity\": 5}, \n                {\"product\": \"Table\", \"total_quantity\": 2}]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. <strong>\u4f18\u5316\u6c47\u603b\u5206\u6790\u7684\u6027\u80fd<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u7d22\u5f15\uff1a<\/strong><br>\u4e3a\u5e38\u7528\u7684\u5206\u7ec4\u5b57\u6bb5\uff08\u5982 <code>category<\/code>\uff09\u521b\u5efa\u7d22\u5f15\uff0c\u52a0\u901f\u67e5\u8be2\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   CREATE INDEX idx_category ON sales(category);<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u5197\u4f59\u8ba1\u7b97\uff1a<\/strong><br>\u4f7f\u7528 CTE \u6216\u5b50\u67e5\u8be2\u51cf\u5c11\u91cd\u590d\u7684\u805a\u5408\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u5206\u5e03\u5f0f\u67e5\u8be2\uff1a<\/strong><br>\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u573a\u666f\u4e2d\uff0c\u4f7f\u7528\u652f\u6301\u5206\u5e03\u5f0f\u8ba1\u7b97\u7684\u6570\u636e\u5e93\uff08\u5982 Google BigQuery\u3001Apache Hive\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u662f SQL \u6c47\u603b\u5206\u6790\u7684\u8be6\u7ec6\u8bb2\u89e3\uff0c\u5305\u62ec\u5e38\u7528\u65b9\u6cd5\u4e0e\u6700\u65b0\u6280\u672f\u7684\u7ed3\u5408\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL \u7684\u6c47\u603b\u5206\u6790\u529f\u80fd\u5f3a\u5927\uff0c\u4e3b\u8981\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u7edf\u8ba1\u3001\u5206\u7ec4\u3001\u805a\u5408\u548c\u7b5b\u9009\u3002\u6c47\u603b\u5206\u6790\u5728\u62a5\u8868\u3001\u6570\u636e\u6316\u6398\u3001\u5546\u4e1a\u667a\u80fd\u4e2d\u975e\u5e38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/445","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=445"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/445\/revisions\/446"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}