{"id":201101,"date":"2025-05-06T11:42:09","date_gmt":"2025-05-06T03:42:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201101\/"},"modified":"2025-05-06T11:42:09","modified_gmt":"2025-05-06T03:42:09","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%a4%9a%e6%9d%a1%e6%9f%a5%e8%af%a2%e5%90%8c%e4%b8%80%e8%a1%a8%e4%b8%8d%e5%90%8c%e5%88%86%e7%bb%84%e7%bb%93%e6%9e%9c%e7%9a%84sql%e8%af%ad%e5%8f%a5%e5%90%88%e5%b9%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201101\/","title":{"rendered":"\u5982\u4f55\u5c06\u591a\u6761\u67e5\u8be2\u540c\u4e00\u8868\u4e0d\u540c\u5206\u7ec4\u7ed3\u679c\u7684SQL\u8bed\u53e5\u5408\u5e76\u6210\u4e00\u6761\u8bed\u53e5\u6267\u884c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5c06\u591a\u6761\u67e5\u8be2\u540c\u4e00\u8868\u4e0d\u540c\u5206\u7ec4\u7ed3\u679c\u7684SQL\u8bed\u53e5\u5408\u5e76\u6210\u4e00\u6761\u8bed\u53e5\u6267\u884c\uff1f<\/h1>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u4e3b\u673a\u5b9d\u8d1d\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u5c06\u591a\u6761\u67e5\u8be2\u540c\u4e00\u8868\u4e0d\u540c\u5206\u7ec4\u7ed3\u679c\u7684SQL\u8bed\u53e5\u5408\u5e76\u6210\u4e00\u6761\u8bed\u53e5\u6267\u884c\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733042930674c22f24cc9a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mysql\u8bed\u53e5\u5408\u5e76\u4f18\u5316<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u7ed9\u5b9a\u591a\u6761\u67e5\u8be2\u540c\u4e00\u8868\u4e0d\u540c\u5206\u7ec4\u7ed3\u679c\u7684sql\u8bed\u53e5\uff0c\u80fd\u5426\u5c06\u5176\u5408\u5e76\u6210\u4e00\u6761sql\u8bed\u53e5\u6267\u884c\uff1f<\/p>\n<pre>select *, count(*) as count from t_search where mark = 'a' group by title order by count desc limit 0, 20\n\nselect *, count(*) as count from t_search where mark = 'b' group by title order by count desc limit 0, 20\n\nselect *, count(*) as count from t_search where mark = 'c' group by title order by count desc limit 0, 20\n\n...<\/pre>\n<p>\u603b\u5171\u670924\u6761sql\u8bed\u53e5\uff0c\u6bcf\u4e2a\u67e5\u8be2\u7684mark\u5b57\u6bb5\u4ecea\u5230z\uff0c\u6bcf\u6761\u8bed\u53e5\u67e5\u8be220\u6761\u7ed3\u679c\u3002<\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p><strong>\u65b9\u6cd51\uff1amysql 8.0+<\/strong><\/p>\n<p>\u4f7f\u7528with\u5b50\u53e5\u548c\u7a97\u53e3\u51fd\u6570\uff1a<\/p>\n<pre>with ranked_data as (\n    select *,\n           count(*) over (partition by title, mark) as count,\n           row_number() over (partition by mark order by count(*) desc) as row_num\n    from t_search\n    where mark between 'a' and 'z'\n    group by title, mark\n)\nselect *\nfrom ranked_data\nwhere row_num &lt;= 20\norder by mark, count desc;<\/pre>\n<p><strong>\u65b9\u6cd52\uff1amysql 8.0\u4ee5\u4e0b<\/strong><\/p>\n<p>\u4f7f\u7528\u53d8\u91cf\u548c\u5b50\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT *\nFROM (\n    SELECT *,\n           @rank := IF(@prev_mark = mark, @rank + 1, 1) AS rank,\n           @prev_mark := mark,\n           COUNT(*) AS count\n    FROM t_search\n    JOIN (SELECT @rank := 0, @prev_mark := '') AS vars\n    WHERE mark BETWEEN 'a' AND 'z'\n    GROUP BY title, mark\n    ORDER BY mark, count DESC\n) AS ranked_data\nWHERE rank &lt;= 20\nORDER BY mark, count DESC;<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5c06\u591a\u6761\u67e5\u8be2\u540c\u4e00\u8868\u4e0d\u540c\u5206\u7ec4\u7ed3\u679c\u7684SQL\u8bed\u53e5\u5408\u5e76\u6210\u4e00\u6761\u8bed\u53e5\u6267\u884c\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06\u591a\u6761\u67e5\u8be2\u540c\u4e00\u8868\u4e0d\u540c\u5206\u7ec4\u7ed3\u679c&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201101","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201101"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201101\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}