{"id":27609,"date":"2024-11-24T08:12:53","date_gmt":"2024-11-24T00:12:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27609\/"},"modified":"2024-11-24T08:12:53","modified_gmt":"2024-11-24T00:12:53","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-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27609\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173129599278757.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528\u5355\u6761 SQL \u8bed\u53e5\u5408\u5e76\u5927\u91cf\u7c7b\u4f3c\u7684\u91cd\u590d\u67e5\u8be2\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528\u5355\u6761 SQL \u8bed\u53e5\u5408\u5e76\u5927\u91cf\u7c7b\u4f3c\u7684\u91cd\u590d\u67e5\u8be2\uff1f\u63d2\u56fe\" \/><\/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  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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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\u5982\u4f55\u7528\u5355\u6761 SQL \u8bed\u53e5\u5408\u5e76\u5927\u91cf\u7c7b\u4f3c\u7684\u91cd\u590d\u67e5\u8be2\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u5355\u6761 sql \u8bed\u53e5\u5b9e\u73b0\u5927\u91cf\u91cd\u590d\u67e5\u8be2 \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 \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 \u89e3\u51b3\u65b9\u6848\uff1a \u65b9\u6cd5 1\uff08\u9002\u7528\u4e8e 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff09\uff1a with ranked_data as ( select *, count(*) over (partition by title, mark) as count, row_number() over (partition by mark order by count(*) desc) as row_num from t_search where mark between &#8216;a&#8217; and &#8216;z&#8217; [&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-27609","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27609","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=27609"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27609\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}