{"id":200707,"date":"2025-05-06T08:33:23","date_gmt":"2025-05-06T00:33:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200707\/"},"modified":"2025-05-06T08:33:23","modified_gmt":"2025-05-06T00:33:23","slug":"%e5%a6%82%e4%bd%95%e7%94%a8%e5%8d%95%e6%9d%a1-sql-%e8%af%ad%e5%8f%a5%e5%90%88%e5%b9%b6%e5%a4%a7%e9%87%8f%e7%b1%bb%e4%bc%bc%e7%9a%84%e9%87%8d%e5%a4%8d%e6%9f%a5%e8%af%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200707\/","title":{"rendered":"\u5982\u4f55\u7528\u5355\u6761 SQL \u8bed\u53e5\u5408\u5e76\u5927\u91cf\u7c7b\u4f3c\u7684\u91cd\u590d\u67e5\u8be2\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u7528\u5355\u6761 SQL \u8bed\u53e5\u5408\u5e76\u5927\u91cf\u7c7b\u4f3c\u7684\u91cd\u590d\u67e5\u8be2\uff1f<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6570\u636e\u5e93\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u7528\u5355\u6761 SQL \u8bed\u53e5\u5408\u5e76\u5927\u91cf\u7c7b\u4f3c\u7684\u91cd\u590d\u67e5\u8be2\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/17313860186732daa2af418.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u901a\u8fc7\u5355\u6761 sql \u8bed\u53e5\u5b9e\u73b0\u5927\u91cf\u91cd\u590d\u67e5\u8be2<\/strong><\/p>\n<p>\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u5f53\u9700\u8981\u91cd\u590d\u6267\u884c\u5927\u91cf\u51e0\u4e4e\u76f8\u540c\u7684\u67e5\u8be2\u65f6\uff0c\u4f7f\u7528\u5355\u72ec\u7684\u67e5\u8be2\u8bed\u53e5\u4f1a\u9020\u6210\u6548\u7387\u4f4e\u4e0b\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5355\u6761 sql \u8bed\u53e5\u5408\u5e76\u591a\u4e2a\u7c7b\u4f3c\u7684\u67e5\u8be2\u3002<\/p>\n<p>\u95ee\u9898\uff1a\u6839\u636e\u7ed9\u5b9a\u793a\u4f8b\uff0c\u5171\u6709 24 \u6761\u7c7b\u4f3c\u7684 sql \u8bed\u53e5\uff0c\u6bcf\u6761\u8bed\u53e5\u90fd\u9488\u5bf9\u4e0d\u540c\u7684 mark \u503c\u6267\u884c\u76f8\u540c\u7684\u67e5\u8be2\uff0c\u4ece t_search \u8868\u4e2d\u6309 title \u5206\u7ec4\u5e76\u6309\u8ba1\u6570\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff08\u9002\u7528\u4e8e mysql 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff09\uff1a<\/strong><\/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\u6cd5 2\uff08\u9002\u7528\u4e8e\u8f83\u4f4e\u7248\u672c\u7684 mysql\uff09\uff1a<\/strong><\/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>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u66f4\u7b80\u6d01\u3001\u66f4\u9ad8\u6548\u7684\u5355\u6761 sql \u8bed\u53e5\u5408\u5e76\u8fd9\u4e9b\u91cd\u590d\u7684\u67e5\u8be2\uff0c\u4ece\u800c\u63d0\u9ad8\u6570\u636e\u5e93\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u7528\u5355\u6761 SQL \u8bed\u53e5\u5408\u5e76\u5927\u91cf\u7c7b\u4f3c\u7684\u91cd\u590d\u67e5\u8be2\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528\u5355\u6761 SQL \u8bed\u53e5\u5408\u5e76\u5927\u91cf&#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-200707","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200707","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=200707"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200707\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}