{"id":5217,"date":"2023-11-17T16:56:09","date_gmt":"2023-11-17T08:56:09","guid":{"rendered":"https:\/\/white-night.club\/?p=5217"},"modified":"2023-11-17T17:26:33","modified_gmt":"2023-11-17T09:26:33","slug":"db16","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/11\/17\/db16\/","title":{"rendered":"MySQL\u4f8b\u9898\uff1a\u4e1a\u52a1\u67e5\u8be2"},"content":{"rendered":"\n<p>\u5927\u81f4\u56de\u987e\u4e00\u4e0b\u601d\u8def\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color has-link-color wp-elements-287a292c3ba355d4aafba0a7a5388826\">\n<p>\u7b2c1\u5173\uff1a\u67e5\u8be2\u67d0\u7f51\u7ad9\u6bcf\u4e2a\u65e5\u671f\u65b0\u7528\u6237\u7684\u6b21\u65e5\u7559\u5b58\u7387<\/p>\n<cite><em>\u6b21\u65e5\uff0c\u65b0\u7528\u6237<\/em><\/cite><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4efb\u52a1\u63cf\u8ff0\">\u4efb\u52a1\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u672c\u5173\u4efb\u52a1\uff1a\u67e5\u8be2\u67d0\u7f51\u7ad9\u6bcf\u4e2a\u65e5\u671f\u65b0\u7528\u6237\u7684\u6b21\u65e5\u7559\u5b58\u7387\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u76f8\u5173\u77e5\u8bc6\">\u76f8\u5173\u77e5\u8bc6<\/h3>\n\n\n\n<p>\u67d0\u7f51\u7ad9\u6bcf\u5929\u6709\u5f88\u591a\u4eba\u767b\u5f55\uff0c\u8bf7\u4f60\u7edf\u8ba1\u4e00\u4e0b\u8be5\u7f51\u7ad9\u6bcf\u4e2a\u65e5\u671f\u65b0\u7528\u6237\u7684\u6b21\u65e5\u7559\u5b58\u7387\u3002 \u6709\u4e00\u4e2a\u767b\u5f55(login)\u8bb0\u5f55\u8868\uff0c\u7b80\u51b5\u5982\u4e0b:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter has-custom-border\"><img decoding=\"async\" src=\"https:\/\/data.educoder.net\/api\/attachments\/2302013\" alt=\",\" style=\"border-style:none;border-width:0px;border-radius:0px\"\/><\/figure>\n\n\n\n<p>\u7b2c1\u884c\u8868\u793auser_id\u4e3a2\u7684\u7528\u6237\u57282020-10-12\u4f7f\u7528\u4e86\u5ba2\u6237\u7aefid\u4e3a1\u7684\u8bbe\u5907\u767b\u5f55\u4e86\u8be5\u7f51\u7ad9\uff0c\u56e0\u4e3a\u662f\u7b2c1\u6b21\u767b\u5f55\uff0c\u6240\u4ee5\u662f\u65b0\u7528\u6237\u3002<\/p>\n\n\n\n<p>\u7b2c4\u884c\u8868\u793auser_id\u4e3a2\u7684\u7528\u6237\u57282020-10-13\u4f7f\u7528\u4e86\u5ba2\u6237\u7aefid\u4e3a2\u7684\u8bbe\u5907\u767b\u5f55\u4e86\u8be5\u7f51\u7ad9\uff0c\u56e0\u4e3a\u662f\u7b2c2\u6b21\u767b\u5f55\uff0c\u6240\u4ee5\u662f\u8001\u7528\u6237\u3002<\/p>\n\n\n\n<p>\u6700\u540e1\u884c\u8868\u793auser_id\u4e3a4\u7684\u7528\u6237\u57282020-10-15\u4f7f\u7528\u4e86\u5ba2\u6237\u7aefid\u4e3a1\u7684\u8bbe\u5907\u767b\u5f55\u4e86\u8be5\u7f51\u7ad9\uff0c\u56e0\u4e3a\u662f\u7b2c2\u6b21\u767b\u5f55\uff0c\u6240\u4ee5\u662f\u8001\u7528\u6237\u3002<\/p>\n\n\n\n<p><strong>\u8bf7\u4f60\u5199\u51fa\u4e00\u4e2asql\u8bed\u53e5\u67e5\u8be2\u6bcf\u4e2a\u65e5\u671f\u65b0\u7528\u6237\u7684\u6b21\u65e5\u7559\u5b58\u7387\uff0c\u7ed3\u679c\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u97623\u4f4d\u6570(3\u4f4d\u4e4b\u540e\u7684\u56db\u820d\u4e94\u5165)\uff0c\u5e76\u4e14\u67e5\u8be2\u7ed3\u679c\u6309\u7167\u65e5\u671f\u5347\u5e8f\u6392\u5e8f<\/strong>\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u67e5\u8be2\u7ed3\u679c\u5982\u4e0b:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter has-custom-border\"><img decoding=\"async\" src=\"https:\/\/data.educoder.net\/api\/attachments\/2302018\" alt=\",\" style=\"border-style:none;border-width:0px;border-radius:0px\"\/><\/figure>\n\n\n\n<p>\u67e5\u8be2\u7ed3\u679c\u8868\u660e: 2020-10-12\u767b\u5f55\u4e863\u4e2a(user_id\u4e3a2\uff0c3\uff0c1)\u65b0\u7528\u6237\uff0c2020-10-13\uff0c\u53ea\u67092\u4e2a(id\u4e3a2,1)\u767b\u5f55\uff0c\u65452020-10-12\u65b0\u7528\u6237\u6b21\u65e5\u7559\u5b58\u7387\u4e3a2\/3=0.667; 2020-10-13\u6ca1\u6709\u65b0\u7528\u6237\u767b\u5f55\uff0c\u8f93\u51fa0.000; 2020-10-14\u767b\u5f55\u4e861\u4e2a(user_id\u4e3a4)\u65b0\u7528\u6237\uff0c2020-10-15\uff0cuser_id\u4e3a4\u7684\u7528\u6237\u767b\u5f55\uff0c\u65452020-10-14\u65b0\u7528\u6237\u6b21\u65e5\u7559\u5b58\u7387\u4e3a1\/1=1.000; 2020-10-15\u6ca1\u6709\u65b0\u7528\u6237\u767b\u5f55\uff0c\u8f93\u51fa0.000; <strong>\u63d0\u793a\uff1a<\/strong> 1\u3001\u672c\u9898\u7684\u65b0\u7528\u6237\u4e0d\u662f\u771f\u6b63\u7684\u65b0\u7528\u6237\u5b9a\u4e49\uff0c\u662f\u672c\u9898\u7ed9\u51fa\u7684\u65b0\u7528\u6237\u5b9a\u4e49\u3002<\/p>\n\n\n\n<p>2\u3001MYSQL\u91cc\u8ba1\u7b97\u65e5\u671ft2\u4e0e\u65e5\u671ft1\u5dee\u7684\u51fd\u6570\u4e3a:datediff(t2,t1)\u3002<\/p>\n\n\n\n<p>3\u3001round(x,y):\u6309y\u4f4d\u5c0f\u6570\uff0c\u5bf9x\u56db\u820d\u4e94\u5165\u3002<\/p>\n\n\n\n<p>4\u3001ifnull\u51fd\u6570\u7684\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<ol>\n<li><code>ifnull(expression_1,expression_2);<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u5982\u679cexpression_1\u4e0d\u4e3aNULL\uff0c\u5219ifnull\u51fd\u6570\u8fd4\u56deexpression_1; \u5426\u5219\u8fd4\u56deexpression_2\u7684\u7ed3\u679c\u3002ifnull\u51fd\u6570\u6839\u636e\u4f7f\u7528\u7684\u4e0a\u4e0b\u6587\u8fd4\u56de\u5b57\u7b26\u4e32\u6216\u6570\u5b57\u3002\u5982\u679c\u8981\u8fd4\u56de\u57fa\u4e8etrue\u6216false\u6761\u4ef6\u7684\u503c\uff0c\u800c\u4e0d\u662fnull\uff0c\u5219\u5e94\u4f7f\u7528if\u51fd\u6570\u3002<\/p>\n\n\n\n<p>5\u3001MySQL\u8bed\u53e5with\u662f MySQL 8.0\u4e2d\u7684\u4e00\u4e2a\u65b0\u7279\u6027\uff0c\u7528\u4e8e\u5e2e\u52a9\u7b80\u5316\u590d\u6742\u67e5\u8be2\u4ee5\u53ca\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002\u5b83\u662f\u4e00\u79cd\u4e34\u65f6\u8868\u7684\u65b9\u5f0f\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u96c6\u53ef\u4ee5\u4f5c\u4e3a\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\u3002\u5728with\u8bed\u53e5\u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u522b\u540d\uff0c\u7136\u540e\u5d4c\u5957\u4f7f\u7528\u3002<\/p>\n\n\n\n<ol>\n<li><code>with t1 as (<\/code><\/li>\n\n\n\n<li><code>select col1 from table1<\/code><\/li>\n\n\n\n<li><code>),<\/code><\/li>\n\n\n\n<li><code>t2 as (<\/code><\/li>\n\n\n\n<li><code>select col1 from table2<\/code><\/li>\n\n\n\n<li><code>)<\/code><\/li>\n\n\n\n<li><code>--\u4f7f\u7528<\/code><\/li>\n\n\n\n<li><code>select * from t1 inner join t2 on t1.col1 = t2.col1;<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u5f20\u8868\uff0ct1\u548ct2\uff0c\u7136\u540e\u4f7f\u7528\u4e86\u8fd9\u4e24\u5f20\u8868\u6765\u8fdb\u884c\u67e5\u8be2\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u907f\u514d\u5728\u67e5\u8be2\u8bed\u53e5\u4e2d\u591a\u6b21\u5d4c\u5957\u4f7f\u7528\u76f8\u540c\u7684\u5b50\u67e5\u8be2\uff0c\u5e76\u4e14\u4e5f\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u67e5\u8be2\u5b9a\u4e49\u4e00\u4e2a\u4e34\u65f6\u8868\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f2\uff1a\u672c\u6b21\u5b9e\u9a8c\u5207\u6362\u5230\u4e86mysql8.0\uff0c\u8981\u6ce8\u610f\u4ee5\u4e0b\u95ee\u9898\uff1a<\/strong> 1\u3001order by\u7684\u5173\u952e\u5b57\u6bb5\uff0c\u8981\u6765\u81easelect\u5173\u952e\u5b57\u6bb5\uff1b 2\u3001group by\u7684\u5173\u952e\u5b57\u6bb5\uff0c\u8981\u548cselect\u5173\u952e\u5b57\u6bb5\u4e00\u81f4\u3002<\/p>\n\n\n\n<p>\u793a\u4f8b1 \u8f93\u5165\uff1a drop table if exists login; create table <code>login<\/code> ( <code>id<\/code> int(4) not null, <code>user_id<\/code> int(4) not null, <code>client_id<\/code> int(4) not null, <code>date<\/code> date not null, primary key (<code>id<\/code>));<\/p>\n\n\n\n<p>insert into login values (1,2,1,&#8217;2020-10-12&#8242;), (2,3,2,&#8217;2020-10-12&#8242;), (3,1,2,&#8217;2020-10-12&#8242;), (4,2,2,&#8217;2020-10-13&#8242;), (5,1,2,&#8217;2020-10-13&#8242;), (6,3,1,&#8217;2020-10-14&#8242;), (7,4,1,&#8217;2020-10-14&#8242;), (8,4,1,&#8217;2020-10-15&#8242;);<\/p>\n\n\n\n<p>\u8f93\u51fa\uff1a date p 2020-10-12 0.667 2020-10-13 0.000 2020-10-14 1.000 2020-10-15 0.000<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5f88\u660e\u663e\uff0c\u5173\u952e\u70b9\u5728\u4e8e\u67e5\u627e\u65b0\u7528\u6237\u548c\u6b21\u65e5\u3002\u6b21\u65e5\u5f88\u660e\u663e\u4e86\uff0c\u76f4\u63a5left join+DATE_ADD\u5c31\u884c\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-vivid-cyan-blue-color has-text-color\">\n<p>\u5199\u8005\u6ce8<\/p>\n<cite>\u4e3a\u4ec0\u4e48\u4e0d\u7528datediff\uff1f\u4e3b\u8981\u662f\u4e4b\u524d\u5199leetcode\u7684\u65f6\u5019\u53d1\u73b0datediff\u65e0\u6cd5\u5904\u74062016-02-29\u548c2016-03-01\u8fd9\u79cd\u60c5\u51b5\u3002\u4f1a\u5bfc\u81f4datediff=NULL\u800c\u975e\u7b49\u4e8e1\u3002\u6240\u4ee5\u6211\u4e2a\u4eba\u4e60\u60ef\u7528DATE_ADD\u6765\u5224\u65ad\u65e5\u671f\u3002<\/cite><\/blockquote>\n\n\n\n<p>\u65b0\u7528\u6237\u6bd4\u8f83\u9ebb\u70e6\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5148\u5bf9login\u8868\u6309\u7528\u6237\u5206\u7ec4\uff0c\u518d\u67e5\u627e\u6bcf\u4e2a\u7528\u6237\u6700\u65e9\u767b\u5f55\u7684\u65e5\u671f\uff08\u5373MIN(date)\uff09\u3002\u518d\u7528\u6c42\u51fa\u6765\u7684\u6700\u5c0f\u503c\u53bb\u5339\u914d\u6765\u8fdb\u884c\u7b5b\u9009\uff0c\u6700\u540e\u5c31\u80fd\u5f97\u5230\u6bcf\u4e2a\u7528\u6237\u6700\u65e9\u767b\u5f55\u7684\u65e5\u671f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USE mydata;\n#\u8bf7\u5728\u6b64\u5904\u6dfb\u52a0\u5b9e\u73b0\u4ee3\u7801\n########## Begin ##########\nSELECT A.date,IFNULL(ROUND(COUNT(DISTINCT E.user_id)\/COUNT(DISTINCT C.user_id),3),0) AS p\nFROM login AS A\nLEFT JOIN(\nSELECT *\nFROM login AS B  \nWHERE B.date=(\n    SELECT MIN(D.date)\n    FROM login AS D \n    WHERE B.user_id=D.user_id\n    GROUP BY D.user_id\n    LIMIT 1\n)) AS C\nON A.date=C.date AND A.user_id=C.user_id\nLEFT JOIN login AS E \nON A.user_id=E.user_id AND DATE_ADD(A.date,INTERVAL 1 DAY)=E.date\nGROUP BY A.date\nORDER BY A.date ASC\n########## End ##########<\/code><\/pre>\n\n\n\n<p>\u5176\u5b83\u7684\u5c31\u6ca1\u5565\u4e86\uff0cLEFT JOIN\u4e5f\u662f\u6211\u4e2a\u4eba\u4e60\u60ef\u3002\u4e3b\u8981\u662f\u65b9\u4fbf\u805a\u5408\u51fd\u6570\u8ba1\u7b97\u975eNULL\u503c\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color has-link-color wp-elements-9046f5c5eb43ab0e95d099911b839830\">\n<p>\u7b2c2\u5173\uff1a\u67e5\u8be2\u6ee1\u8db3\u6761\u4ef6\u7684\u7528\u6237<\/p>\n<cite><em>\u7a97\u53e3\u51fd\u6570+\u6d3e\u751f\u8868<\/em><\/cite><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4efb\u52a1\u63cf\u8ff0\">\u4efb\u52a1\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u6709\u7684\u540c\u5b66\u4f1a\u8d2d\u4e70\u7f51\u4e0a\u8bad\u7ec3\u8bfe\u7a0b\u6765\u5b66\u4e60\uff0c\u67d0\u6559\u80b2\u7f51\u7ad9\u5bf9\u8d2d\u4e70\u8bb0\u5f55\u4f1a\u4ea7\u751f\u8ba2\u5355\u5b58\u5230\u6570\u636e\u5e93\uff0c\u6709\u4e00\u4e2a\u8ba2\u5355\u4fe1\u606f\u8868(order_info)\uff0c\u8868\u7684\u90e8\u5206\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter has-custom-border\"><img decoding=\"async\" src=\"https:\/\/data.educoder.net\/api\/attachments\/7277964?type=image\/png\" alt=\",\" style=\"border-style:none;border-width:0px;border-radius:0px\"\/><\/figure>\n\n\n\n<p>\u7b2c1\u884c\u8868\u793auser_id\u4e3a557336\u7684\u7528\u6237\u57282021-10-10\u7684\u65f6\u5019\u4f7f\u7528\u4e86client_id\u4e3a1\u7684\u5ba2\u6237\u7aef\u4e0b\u4e86C++\u8bfe\u7a0b\u7684\u8ba2\u5355\uff0c\u4f46\u662f\u72b6\u6001\u4e3a\u6ca1\u6709\u8d2d\u4e70\u6210\u529f\u3002\u7b2c2\u884c\u4e3a\u8d2d\u4e70\u6210\u529f\u3002<\/p>\n\n\n\n<p><strong>\u67e5\u8be2\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684\u7528\u6237\uff1a \u57282021-10-15\u4ee5\u540e\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u7528\u6237\u4e0b\u53552\u4e2a\u4ee5\u53ca2\u4e2a\u4ee5\u4e0a\u72b6\u6001\u4e3a\u8d2d\u4e70\u6210\u529f\u7684C++\u8bfe\u7a0b\u6216Java\u8bfe\u7a0b\u6216Python\u8bfe\u7a0b\uff0c\u90a3\u4e48\u8f93\u51fa\u8fd9\u4e2a\u7528\u6237\u7684user_id\uff0c\u4ee5\u53ca\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u7b2c\u4e00\u6b21\u8d2d\u4e70\u6210\u529f\u7684\u65e5\u671ffirst_buy_date\uff0c\u4ee5\u53ca\u6ee1\u8db3\u524d\u9762\u6761\u4ef6\u7684\u7b2c\u4e8c\u6b21\u8d2d\u4e70\u6210\u529f\u7684\u65e5\u671fsecond_buy_date\uff0c\u4ee5\u53ca\u8d2d\u4e70\u6210\u529f\u7684\u6b21\u6570cnt\uff0c\u5e76\u4e14\u8f93\u51fa\u7ed3\u679c\u6309\u7167user_id\u5347\u5e8f\u6392\u5e8f\u3002<\/strong><\/p>\n\n\n\n<p><strong>\u51fd\u6570\u63d0\u793a<\/strong> 1\u3001row_number() over(partition by\u67d0\u5b57\u6bb5 order by \u67d0\u5b57\u6bb5)\uff1a\u7a97\u53e3\u51fd\u6570\u6309\u67d0\u5b57\u6bb5\u5206\u7ec4\uff0c\u751f\u6210\u4ece1\u5f00\u59cb\u7684\u987a\u5e8f\u7f16\u53f7\u3002 2\u3001count(*) over(partition by \u67d0\u5b57\u6bb5)\uff1a\u6c42\u5206\u7ec4\u540e\u7684\u603b\u6570\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff0c\u672c\u6b21\u5b9e\u9a8c\u5207\u6362\u5230\u4e86mysql8.0\uff0c\u8981\u6ce8\u610f\u4ee5\u4e0b\u95ee\u9898\uff1a<\/strong> 1\u3001order by\u7684\u5173\u952e\u5b57\u6bb5\uff0c\u8981\u6765\u81easelect\u5173\u952e\u5b57\u6bb5\uff1b 2\u3001group by\u7684\u5173\u952e\u5b57\u6bb5\uff0c\u8981\u548cselect\u5173\u952e\u5b57\u6bb5\u4e00\u81f4\u3002<\/p>\n\n\n\n<p>\u793a\u4f8b1 \u8f93\u5165\uff1a drop table if exists order_info; create table order_info( id int(4) not null, user_id int(4) not null, product_name char(20) not null, status char(20) not null, client_id int(4) not null, date date not null, primary key (id));<\/p>\n\n\n\n<p>insert into order_info values (1,557336,&#8217;C++&#8217;,&#8217;no_completed&#8217;,1,&#8217;2021-10-10&#8242;), (2,230173543,&#8217;Python&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-12&#8242;), (3,230173543,&#8217;Python&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-15&#8242;), (4,557336,&#8217;Java&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-15&#8242;), (5,230173543,&#8217;Java&#8217;,&#8217;no_completed&#8217;,2,&#8217;2021-10-16&#8242;), (6,230173543,&#8217;Java&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-20&#8242;), (7,230173543,&#8217;C++&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-21&#8242;), (8,557336,&#8217;Java&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-21&#8242;), (9,557336,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-22&#8242;), (10,230173543,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-22&#8242;), (11,663466,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;completed&#8217;,3,&#8217;2021-10-22&#8242;), (12,663466,&#8217;C++&#8217;,&#8217;completed&#8217;,3,&#8217;2021-10-22&#8242;), (13,663466,&#8217;Python&#8217;,&#8217;no_completed&#8217;,3,&#8217;2021-10-23&#8242;), (14,663466,&#8217;Python&#8217;,&#8217;completed&#8217;,3,&#8217;2021-10-24&#8242;), (15,663466,&#8217;C++&#8217;,&#8217;completed&#8217;,3,&#8217;2021-10-25&#8242;), (16,8912311,&#8217;Java&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-25&#8242;), (17,3345600,&#8217;\u9ad8\u7b49\u6570\u5b66&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-27&#8242;), (18,3345600,&#8217;\u6570\u5b66\u5206\u6790&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-28&#8242;), (19,3345600,&#8217;\u6570\u636e\u5e93\u7cfb\u7edf&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-28&#8242;), (20,663466,&#8217;C++&#8217;,&#8217;no_completed&#8217;,3,&#8217;2021-10-29&#8242;), (21,557336,&#8217;Python&#8217;,&#8217;no_completed&#8217;,3,&#8217;2021-10-29&#8242;), (22,557336,&#8217;Python&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-29&#8242;), (23,230173543,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;no_completed&#8217;,2,&#8217;2021-10-29&#8242;), (24,663466,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;no_completed&#8217;,3,&#8217;2021-10-29&#8242;), (25,230173543,&#8217;Python&#8217;,&#8217;no_completed&#8217;,2,&#8217;2021-10-30&#8242;), (26,557336,&#8217;Python&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-30&#8242;), (27,823345,&#8217;Python&#8217;,&#8217;no_completed&#8217;,1,&#8217;2021-10-31&#8242;), (28,823345,&#8217;Python&#8217;,&#8217;no_completed&#8217;,1,&#8217;2021-10-31&#8242;), (29,823345,&#8217;Python&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-31&#8242;), (30,230173543,&#8217;Python&#8217;,&#8217;no_completed&#8217;,4,&#8217;2021-10-31&#8242;), (31,3345600,&#8217;\u6570\u636e\u5e93\u7cfb\u7edf&#8217;,&#8217;no_completed&#8217;,1,&#8217;2021-11-01&#8242;);<\/p>\n\n\n\n<p>\u8f93\u51fa\uff1a user_id first_buy_date second_buy_date cnt 557336 2021-10-21 2021-10-29 3 663466 2021-10-22 2021-10-24 3 230173543 2021-10-20 2021-10-21 2<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u8fd9\u9898\u6bd4\u53e6\u5916\u4e24\u9898\u7b80\u5355\u4e9b\u3002\u4e3b\u8981\u662f\u6d3e\u751f\u8868\u3002<\/p>\n\n\n\n<p>\u6211\u4e00\u5f00\u59cb\u60f3\u5728\u6700\u5916\u5c42\u7528COUNT\u53bb\u8ba1\u7b97\u8d2d\u4e70\u6210\u529f\u7684\u6b21\u6570\u3002\u4f46\u7531\u4e8eSQL\u8bed\u53e5\u7684\u6267\u884c\u987a\u5e8f\u91cc\uff0cSELECT\u662f\u6700\u540e\u6267\u884c\u7684\u3002\u6240\u4ee5\u5982\u679c\u4f60\u60f3\u8981\u53ea\u8fd4\u56de\u524d\u4e24\u4e2a\u8d2d\u4e70\u6210\u529f\u7684\u65e5\u671f\u7684\u8bdd\uff08\u8fd9\u6837\u4f60\u624d\u80fd\u7528MIN\u548cMAX\u5f97\u5230\u7b2c\u4e00\u6b21\u548c\u7b2c\u4e8c\u6b21\u6210\u529f\u8d2d\u4e70\u7684\u65e5\u671f\uff09\uff0c\u90a3\u5fc5\u7136\u4f1a\u5728\u6700\u5916\u5c42\u7684WHERE\u4e2d\u5bf9rk\u8fdb\u884c\u7b5b\u9009\uff08\u6bd4\u5982rk\u22642\uff09\u3002\u8fd9\u4e48\u505a\u5c31\u4f1a\u5bfc\u81f4\u6700\u540eCOUNT\u7684\u7ed3\u679c\u4e0d\u5168\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u6211\u9009\u62e9\u5728\u6d3e\u751f\u8868\u7684\u65f6\u5019\u5c31\u7528COUNT OVER\u5bf9id\u5206\u7ec4\u518d\u6c42\u548c\uff0c\u6700\u540e\u76f4\u63a5\u5916\u90e8SELECT\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u8fd8\u662fCOUNT(xxx)OVER\u7684\u7528\u6cd5\u5427\uff0c\u6211\u4e5f\u662f\u5728\u5237leetcode\u9898\u7684\u65f6\u5019\u53d1\u73b0\u805a\u5408\u51fd\u6570\u90fd\u80fd\u5f53\u7a97\u53e3\u51fd\u6570\u7528\u7684\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color has-link-color wp-elements-2ce817a328afc1b637b194091ce3c65d\">\n<p>\u7b2c3\u5173\uff1a\u67e5\u8be2\u672a\u5b8c\u6210\u8ba2\u5355\u7387\u6392\u540d\u524d\u4e09\u7684\u7528\u6237<\/p>\n<cite><em>\u6d3e\u751f\u8868\u5957\u6d3e\u751f\u8868<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u6709\u7684\u540c\u5b66\u4f1a\u8d2d\u4e70\u7f51\u4e0a\u8bad\u7ec3\u8bfe\u7a0b\u6765\u5b66\u4e60\uff0c\u67d0\u6559\u80b2\u7f51\u7ad9\u5bf9\u8d2d\u4e70\u8bb0\u5f55\u4f1a\u4ea7\u751f\u8ba2\u5355\u5b58\u5230\u6570\u636e\u5e93\uff0c\u6709\u4e00\u4e2a\u8ba2\u5355\u4fe1\u606f\u8868(order_info)\uff0c\u8868\u7684\u90e8\u5206\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter has-custom-border\"><img decoding=\"async\" src=\"https:\/\/data.educoder.net\/api\/attachments\/7277964?type=image\/png\" alt=\",\" style=\"border-style:none;border-width:0px;border-radius:0px\"\/><\/figure>\n\n\n\n<p>\u7b2c1\u884c\u8868\u793auser_id\u4e3a557336\u7684\u7528\u6237\u57282021-10-10\u7684\u65f6\u5019\u4f7f\u7528\u4e86client_id\u4e3a1\u7684\u5ba2\u6237\u7aef\u4e0b\u4e86C++\u8bfe\u7a0b\u7684\u8ba2\u5355\uff0c\u4f46\u662f\u72b6\u6001\u4e3a\u6ca1\u6709\u8d2d\u4e70\u6210\u529f\u3002\u7b2c2\u884c\u4e3a\u8d2d\u4e70\u6210\u529f\u3002<\/p>\n\n\n\n<p><strong>\u67e5\u8be2\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684\u7528\u6237\uff1a \u627e\u51fa\u53cc11\u9884\u70ed\u671f\u95f4\uff0c(2021.10.16-2021.10.31)\uff0c\u5404product_name(\u8981\u5448\u73b0)\u7684\u7528\u6237\u672a\u5b8c\u6210\u8ba2\u5355\u7387(%\u540e\u663e\u793a2\u4f4d\u5c0f\u6570)\u6392\u524d\u4e09\u540d(\u964d\u5e8f\u6392\u5e8f\uff0c\u4e0d\u4e3a0\uff0c\u4e14\u7528dense_rank\u51fd\u6570)\u7684user_id\u3001rnk(\u540d\u6b21)\u3001incomp_rate(\u672a\u5b8c\u6210\u8ba2\u5355\u7387)\uff0c\u5e76\u5148\u6309product_name\u5347\u5e8f\u6392\u5e8f,\u518d\u6309rnk\u5347\u5e8f\u6392\u5e8f\u3002<\/strong><\/p>\n\n\n\n<p><strong>\u51fd\u6570\u63d0\u793a<\/strong> 1\u3001round(x,y):\u6309y\u4f4d\u5c0f\u6570\uff0c\u5bf9x\u56db\u820d\u4e94\u5165\u3002<\/p>\n\n\n\n<p>2\u3001concat(s1,s2&#8230;sn):\u5b57\u7b26\u4e32s1,s2\u7b49\u591a\u4e2a\u5b57\u7b26\u4e32\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>3\u3001dense_rank() over(partition by a\u5b57\u6bb5order by b\u5b57\u6bb5)\uff1a\u7a97\u53e3\u51fd\u6570\u6309a\u5b57\u6bb5\u5206\u7ec4\uff0c\u6309b\u5b57\u6bb5\u6392\u5e8f(\u7f3a\u7701\u5347\u5e8f)\u751f\u6210\u4ece1\u5f00\u59cb\u6570\u503c\u7f16\u53f7\uff0c\u591a\u503c\u5e76\u5217\u4f46\u6392\u540d\u4ec5+1\u3002 (\u8bf4\u660e\uff1a\u548crank() over \u7684\u4f5c\u7528\u76f8\u540c\uff0c\u533a\u522b\u5728\u4e8edense_rank() over \u6392\u540d\u662f\u5bc6\u96c6\u8fde\u7eed\u7684\u3002\u4f8b\u5982\u5b66\u751f\u6392\u540d\uff0c\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u6210\u7ee9\u76f8\u540c\u7684\u4e24\u540d\u662f\u5e76\u5217\uff0c\u4e0b\u4e00\u4f4d\u540c\u5b66\u63a5\u7740\u4e0b\u4e00\u4e2a\u540d\u6b21\u3002\u5373\uff1a1 1 2 3 4 5 5 6)<\/p>\n\n\n\n<p>4\u3001if\u51fd\u6570\u662fMySQL\u4e2d\u7684\u6761\u4ef6\u51fd\u6570\u4e4b\u4e00\uff0c\u7528\u4e8e\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u5426\u5219\u8fd4\u56de\u53e6\u4e00\u4e2a\u503c\u3002IF\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol>\n<li><code>if(condition, true_value, false_value)<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u5176\u4e2d\uff0ccondition\u662f\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0ctrue_value\u662f\u6ee1\u8db3\u6761\u4ef6\u65f6\u8fd4\u56de\u7684\u503c\uff0cfalse_value\u662f\u4e0d\u6ee1\u8db3\u6761\u4ef6\u65f6\u8fd4\u56de\u7684\u503c\u3002<\/p>\n\n\n\n<p>5\u3001\u5728MySQL\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528if\u548csum\u51fd\u6570\u7684\u7ec4\u5408\u6765\u5b9e\u73b0\u591a\u6761\u4ef6\u8ba1\u7b97\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a&#8221;orders&#8221;\u7684\u8868\u683c\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u8ba2\u5355\u4fe1\u606f\uff0c\u5305\u62ec\u8ba2\u5355ID\u3001\u5ba2\u6237ID\u3001\u8ba2\u5355\u91d1\u989damount\u548c\u8ba2\u5355\u72b6\u6001status\u3002\u73b0\u5728\u6211\u4eec\u8981\u8ba1\u7b97\u4e0d\u540c\u72b6\u6001\u8ba2\u5355\u7684\u603b\u91d1\u989d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7684MySQL\u67e5\u8be2\u8bed\u53e5\uff1a<\/p>\n\n\n\n<ol>\n<li><code>select<\/code><\/li>\n\n\n\n<li><code>sum(if(status = 'pending', amount, 0)) as pending_amount,<\/code><\/li>\n\n\n\n<li><code>sum(if(status = 'completed', amount, 0)) as completed_amount,<\/code><\/li>\n\n\n\n<li><code>sum(if(status = 'canceled', amount, 0)) as canceled_amount<\/code><\/li>\n\n\n\n<li><code>from<\/code><\/li>\n\n\n\n<li><code>orders;<\/code><\/li>\n<\/ol>\n\n\n\n<p>6\u3001MySQL\u8bed\u53e5with\u662f MySQL 8.0\u4e2d\u7684\u4e00\u4e2a\u65b0\u7279\u6027\uff0c\u7528\u4e8e\u5e2e\u52a9\u7b80\u5316\u590d\u6742\u67e5\u8be2\u4ee5\u53ca\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002\u5b83\u662f\u4e00\u79cd\u4e34\u65f6\u8868\u7684\u65b9\u5f0f\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u96c6\u53ef\u4ee5\u4f5c\u4e3a\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\u3002\u5728with\u8bed\u53e5\u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u522b\u540d\uff0c\u7136\u540e\u5d4c\u5957\u4f7f\u7528\u3002<\/p>\n\n\n\n<ol>\n<li><code>with t1 as (<\/code><\/li>\n\n\n\n<li><code>select col1 from table1<\/code><\/li>\n\n\n\n<li><code>),<\/code><\/li>\n\n\n\n<li><code>t2 as (<\/code><\/li>\n\n\n\n<li><code>select col1 from table2<\/code><\/li>\n\n\n\n<li><code>)<\/code><\/li>\n\n\n\n<li><code>--\u4f7f\u7528<\/code><\/li>\n\n\n\n<li><code>select * from t1 inner join t2 on t1.col1 = t2.col1;<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u5f20\u8868\uff0ct1\u548ct2\uff0c\u7136\u540e\u4f7f\u7528\u4e86\u8fd9\u4e24\u5f20\u8868\u6765\u8fdb\u884c\u67e5\u8be2\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u907f\u514d\u5728\u67e5\u8be2\u8bed\u53e5\u4e2d\u591a\u6b21\u5d4c\u5957\u4f7f\u7528\u76f8\u540c\u7684\u5b50\u67e5\u8be2\uff0c\u5e76\u4e14\u4e5f\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u67e5\u8be2\u5b9a\u4e49\u4e00\u4e2a\u4e34\u65f6\u8868\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff0c\u672c\u6b21\u5b9e\u9a8c\u5207\u6362\u5230\u4e86mysql8.0\uff0c\u8981\u6ce8\u610f\u4ee5\u4e0b\u95ee\u9898\uff1a<\/strong> 1\u3001order by\u7684\u5173\u952e\u5b57\u6bb5\uff0c\u8981\u6765\u81easelect\u5173\u952e\u5b57\u6bb5\uff1b 2\u3001group by\u7684\u5173\u952e\u5b57\u6bb5\uff0c\u8981\u548cselect\u5173\u952e\u5b57\u6bb5\u4e00\u81f4.<\/p>\n\n\n\n<p>\u793a\u4f8b1 \u8f93\u5165\uff1a drop table if exists order_info; create table order_info( id int(4) not null, user_id int(4) not null, product_name char(20) not null, status char(20) not null, client_id int(4) not null, date date not null, primary key (id));<\/p>\n\n\n\n<p>insert into order_info values (1,557336,&#8217;C++&#8217;,&#8217;no_completed&#8217;,1,&#8217;2021-10-10&#8242;), (2,230173543,&#8217;Python&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-12&#8242;), (3,230173543,&#8217;Python&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-15&#8242;), (4,557336,&#8217;Java&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-15&#8242;), (5,230173543,&#8217;Java&#8217;,&#8217;no_completed&#8217;,2,&#8217;2021-10-16&#8242;), (6,230173543,&#8217;Java&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-20&#8242;), (7,230173543,&#8217;C++&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-21&#8242;), (8,557336,&#8217;Java&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-21&#8242;), (9,557336,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-22&#8242;), (10,230173543,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-22&#8242;), (11,663466,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;completed&#8217;,3,&#8217;2021-10-22&#8242;), (12,663466,&#8217;C++&#8217;,&#8217;completed&#8217;,3,&#8217;2021-10-22&#8242;), (13,663466,&#8217;Python&#8217;,&#8217;no_completed&#8217;,3,&#8217;2021-10-23&#8242;), (14,663466,&#8217;Python&#8217;,&#8217;completed&#8217;,3,&#8217;2021-10-24&#8242;), (15,663466,&#8217;C++&#8217;,&#8217;completed&#8217;,3,&#8217;2021-10-25&#8242;), (16,8912311,&#8217;Java&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-25&#8242;), (17,3345600,&#8217;\u9ad8\u7b49\u6570\u5b66&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-27&#8242;), (18,3345600,&#8217;\u6570\u5b66\u5206\u6790&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-28&#8242;), (19,3345600,&#8217;\u6570\u636e\u5e93\u7cfb\u7edf&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-28&#8242;), (20,663466,&#8217;C++&#8217;,&#8217;no_completed&#8217;,3,&#8217;2021-10-29&#8242;), (21,557336,&#8217;Python&#8217;,&#8217;no_completed&#8217;,3,&#8217;2021-10-29&#8242;), (22,557336,&#8217;Python&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-29&#8242;), (23,230173543,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;no_completed&#8217;,2,&#8217;2021-10-29&#8242;), (24,663466,&#8217;\u79bb\u6563\u6570\u5b66&#8217;,&#8217;no_completed&#8217;,3,&#8217;2021-10-29&#8242;), (25,230173543,&#8217;Python&#8217;,&#8217;no_completed&#8217;,2,&#8217;2021-10-30&#8242;), (26,557336,&#8217;Python&#8217;,&#8217;completed&#8217;,2,&#8217;2021-10-30&#8242;), (27,823345,&#8217;Python&#8217;,&#8217;no_completed&#8217;,1,&#8217;2021-10-31&#8242;), (28,823345,&#8217;Python&#8217;,&#8217;no_completed&#8217;,1,&#8217;2021-10-31&#8242;), (29,823345,&#8217;Python&#8217;,&#8217;completed&#8217;,1,&#8217;2021-10-31&#8242;), (30,230173543,&#8217;Python&#8217;,&#8217;no_completed&#8217;,4,&#8217;2021-10-31&#8242;), (31,3345600,&#8217;\u6570\u636e\u5e93\u7cfb\u7edf&#8217;,&#8217;no_completed&#8217;,1,&#8217;2021-11-01&#8242;);<\/p>\n\n\n\n<p>\u8f93\u51fa\uff1a product_name user_id rnk incomp_rate C++ 663466 1 33.33% Java 230173543 1 50.00% Python 230173543 1 100.00% Python 823345 2 66.67% Python 663466 3 50.00% \u79bb\u6563\u6570\u5b66 230173543 1 50.00% \u79bb\u6563\u6570\u5b66 663466 1 50.00%<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5176\u5b9e\u5c31\u662f\u4e24\u4e2a\u6d3e\u751f\u8868\u5d4c\u5957\uff08\u4e0d\u77e5\u9053\u80fd\u4e0d\u80fd\u8fd9\u4e48\u53eb\uff09\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u8981\u7ed9\u672a\u5b8c\u6210\u7387\u6392\u5e8f\uff0c\u90a3\u4e48\u4f60\u80af\u5b9a\u5f97\u5148\u7b97\u51fa\u672a\u5b8c\u6210\u7387\u624d\u80fd\u7ed9\u5b83\u6392\u5e8f\u3002\u6bd4\u8f83\u70e6\u4eba\u7684\u5c31\u662f\u7531\u4e8e\u8fd9\u8be5\u6b7b\u7684\u6267\u884c\u987a\u5e8f\uff0cSELECT\u80af\u5b9a\u662f\u6700\u540e\u6267\u884c\u7684\uff0c\u6240\u4ee5\u4f60\u5728SELECT\u4e2d\u5b9a\u4e49\u7684\u522b\u540d\u5728\u5f53\u524d\u7ed3\u679c\u96c6\u8f93\u51fa\u4e4b\u524d\u90fd\u662f\u7528\u4e0d\u4e86\u7684\u3002<\/p>\n\n\n\n<p>\u60f3\u7528\u522b\u540d\u7684\u8bdd\uff0c\u5fc5\u987b\u8981\u5728\u5916\u9762\u518d\u5957\u4e00\u5c42SELECT\u3002\u8fd9\u4e2a\u65f6\u5019\u5c31\u80fd\u5bf9\u672a\u5b8c\u6210\u7387\u7528dense_rank\u5bf9\u672a\u5b8c\u6210\u7387\u7684\u7ed3\u679c\u96c6\u6392\u5e8f\u4e86\u3002<\/p>\n\n\n\n<p>\u603b\u7684\u6765\u8bf4\u5c31\u662f\u4e00\u4e2a\u4e00\u4e2a\u7ed3\u679c\u96c6\u7b5b\u9009\uff0c\u4e0a\u4e00\u4e2a\u7ed3\u679c\u96c6\u901a\u8fc7FROM\u4f5c\u4e3a\u4e0b\u4e00\u4e2a\u7ed3\u679c\u96c6\u7684\u8f93\u5165\u3002\u518d\u7528SELECT\u4f5c\u4e3a\u8f93\u51fa\u7ed9\u4e0b\u4e00\u4e2a\u7ed3\u679c\u96c6\u6216\u8005\u76f4\u63a5\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>CONCAT\u53ef\u4ee5\u6700\u540eSELECT\u8f93\u51fa\u7ed3\u679c\u7684\u65f6\u5019\u518d\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USE mydata;\n#\u8bf7\u5728\u6b64\u5904\u6dfb\u52a0\u5b9e\u73b0\u4ee3\u7801\n########## Begin ##########\nSELECT t2.product_name,t2.user_id,t2.rnk,CONCAT(ROUND(t2.incomp_rate,2),'%') AS incomp_rate\nFROM(\n    SELECT *,dense_rank()over(partition by t.product_name order by t.incomp_rate DESC) AS rnk\n    FROM (\nSELECT A.user_id,A.product_name,ROUND(COUNT(IF(A.status='no_completed',1,NULL))*100\/COUNT(A.status),4) AS incomp_rate\nFROM order_info AS A\nWHERE A.date&gt;='2021-10-16' AND A.date&lt;='2021-10-31'\nAND EXISTS(\n    SELECT 1\n    FROM order_info AS B \n    WHERE B.user_id=A.user_id AND B.product_name=A.product_name\n    AND B.date&gt;='2021-10-16' AND B.date&lt;='2021-10-31'\n    AND B.status='no_completed'\n)\nGROUP BY A.user_id,A.product_name\n    )t\n)AS t2\nWHERE t2.rnk&lt;=3\nORDER BY t2.product_name ASC,t2.rnk ASC\n########## End ##########   <\/code><\/pre>\n\n\n\n<p>\u611f\u89c9\u8fd8\u6709\u5f88\u591a\u5730\u65b9\u53ef\u4ee5\u4f18\u5316\u3002\u561b\uff0c<s>\u660e\u5e74<\/s>\u4ee5\u540e\u518d\u8bf4\u5427\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-medium has-custom-border\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"298\" src=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/11\/discuss_1697595963084-300x298.jpeg\" alt=\"\" class=\"wp-image-5186\" style=\"border-style:none;border-width:0px;border-radius:0px\" srcset=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/11\/discuss_1697595963084-300x298.jpeg 300w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/11\/discuss_1697595963084-150x150.jpeg 150w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/11\/discuss_1697595963084-768x762.jpeg 768w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/11\/discuss_1697595963084.jpeg 967w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u67e5\u8be2\u4f8b\u9898<\/p>\n","protected":false},"author":1,"featured_media":3123,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[69,35],"tags":[99],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/5217"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=5217"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/5217\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/3123"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=5217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=5217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=5217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}