{"id":7920,"date":"2024-11-14T12:12:53","date_gmt":"2024-11-14T04:12:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7920\/"},"modified":"2024-11-14T12:12:53","modified_gmt":"2024-11-14T04:12:53","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:\/\/fwq.ai\/blog\/7920\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173133325366222.jpg\" class=\"aligncenter\" title=\"\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\u63d2\u56fe\" alt=\"\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\u63d2\u56fe\" \/><\/p>\n<p><strong>\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\u7684\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\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\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bed\u53e5\u5408\u5e76\u4f18\u5316 \u95ee\u9898\uff1a\u7ed9\u5b9a\u591a\u6761\u67e5\u8be2\u540c\u4e00\u8868\u4e0d\u540c\u5206\u7ec4\u7ed3\u679c\u7684\uff0c\u80fd\u5426\u5c06\u5176\u5408\u5e76\u6210\u4e00\u6761sql\u8bed\u53e5\u6267\u884c\uff1f select *, count(*) as count from t_search where mark = &#8216;a&#8217; group by title order by count desc limit 0, 20 select *, count(*) as count from t_search where mark = &#8216;b&#8217; group by title order by count desc limit 0, 20 select *, count(*) as count from t_search where mark = &#8216;c&#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-7920","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7920","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=7920"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7920\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}