{"id":6743,"date":"2024-11-14T11:24:39","date_gmt":"2024-11-14T03:24:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6743\/"},"modified":"2024-11-14T11:24:39","modified_gmt":"2024-11-14T03:24:39","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-mysql-%e6%9f%a5%e8%af%a2%e8%ae%a1%e7%ae%97%e5%87%ba%e6%af%8f%e7%af%87%e6%96%87%e7%ab%a0%e8%a2%ab%e5%93%aa%e4%ba%9b%e4%ba%ba%e6%b5%8f%e8%a7%88%e8%bf%87%ef%bc%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6743\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 MySQL \u67e5\u8be2\u8ba1\u7b97\u51fa\u6bcf\u7bc7\u6587\u7ae0\u88ab\u54ea\u4e9b\u4eba\u6d4f\u89c8\u8fc7\uff0c\u5e76\u7edf\u8ba1\u8fd9\u4e9b\u6d4f\u89c8\u8005\u9605\u8bfb\u7684\u5176\u4ed6\u6587\u7ae0\u7684\u6d4f\u89c8\u6b21\u6570\uff0c\u5e76\u8f93\u51fa\u6d4f\u89c8\u6b21\u6570\u6700\u591a\u7684\u524d\u51e0\u7bc7\u6587\u7ae0\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172985381059501.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528 MySQL \u67e5\u8be2\u8ba1\u7b97\u51fa\u6bcf\u7bc7\u6587\u7ae0\u88ab\u54ea\u4e9b\u4eba\u6d4f\u89c8\u8fc7\uff0c\u5e76\u7edf\u8ba1\u8fd9\u4e9b\u6d4f\u89c8\u8005\u9605\u8bfb\u7684\u5176\u4ed6\u6587\u7ae0\u7684\u6d4f\u89c8\u6b21\u6570\uff0c\u5e76\u8f93\u51fa\u6d4f\u89c8\u6b21\u6570\u6700\u591a\u7684\u524d\u51e0\u7bc7\u6587\u7ae0\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528 MySQL \u67e5\u8be2\u8ba1\u7b97\u51fa\u6bcf\u7bc7\u6587\u7ae0\u88ab\u54ea\u4e9b\u4eba\u6d4f\u89c8\u8fc7\uff0c\u5e76\u7edf\u8ba1\u8fd9\u4e9b\u6d4f\u89c8\u8005\u9605\u8bfb\u7684\u5176\u4ed6\u6587\u7ae0\u7684\u6d4f\u89c8\u6b21\u6570\uff0c\u5e76\u8f93\u51fa\u6d4f\u89c8\u6b21\u6570\u6700\u591a\u7684\u524d\u51e0\u7bc7\u6587\u7ae0\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e00\u4e2a sql \u8bf7\u6559\uff1a\u8ba1\u7b97\u5173\u8054\u5185\u5bb9\u7684\u6d4f\u89c8\u91cf<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u5229\u7528  \u67e5\u8be2\u8ba1\u7b97\u51fa\u6bcf\u7bc7\u6587\u7ae0\u88ab\u54ea\u4e9b\u4eba\u6d4f\u89c8\u8fc7\uff0c\u7136\u540e\u518d\u8ba1\u7b97\u51fa\u8fd9\u4e9b\u6d4f\u89c8\u8005\u66fe\u7ecf\u6d4f\u89c8\u8fc7\u7684\u5176\u4ed6\u6587\u7ae0\u7684\u6d4f\u89c8\u6b21\u6570\uff0c\u5e76\u8f93\u51fa\u6d4f\u89c8\u6b21\u6570\u6700\u591a\u7684\u524d\u51e0\u7bc7\u6587\u7ae0\uff1f<\/p>\n<p><strong>sql \u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u63d0\u4f9b\u7684\u793a\u4f8b sql \u53ea\u80fd\u8ba1\u7b97\u7279\u5b9a\u4e00\u7bc7\u5185\u5bb9\u7684\u6570\u636e\u3002\u95ee\u9898\u662f\uff0c\u6211\u4eec\u5e0c\u671b\u5bf9\u6bcf\u7bc7\u6587\u7ae0\u90fd\u8fdb\u884c\u540c\u6837\u7684\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p><strong>\u65b9\u6cd5 1\uff08\u4e0d\u63a8\u8350\uff09<\/strong><\/p>\n<p>\u5982\u679c\u975e\u8981\u4f7f\u7528\u5355\u6761 sql \u8bed\u53e5\u89e3\u51b3\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4ee3\u7801\uff08\u672a\u7ecf\u6d4b\u8bd5\uff09\uff1a<\/p>\n<pre>select\n  l.content_id as l_content_id,\n  t.content_id as t_content_id,\n  count(t.uid) as pv\nfrom read_log as t\njoin (\n  select\n    content_id,\n    group_concat(distinct uid, '') as uid_list\n  from read_log\n  group by\n    content_id\n) as l\n  on find_in_set(t.uid, l.uid_list)\ngroup by\n  l.content_id,\n  t.content_id\norder by\n  l.content_id,\n  pv desc;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff08\u63a8\u8350\uff09\uff1a<\/strong><\/p>\n<p>\u5bf9\u4e8e\u6d77\u91cf\u6570\u636e\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u5355\u6761 sql \u8bed\u53e5\u3002\u66f4\u597d\u7684\u505a\u6cd5\u662f\u4f7f\u7528\u5b9a\u65f6\u5f02\u6b65\u4efb\u52a1\u9010\u6b65\u7edf\u8ba1\u6570\u636e\u3002<\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u4f7f\u7528 sql \u4e00\u6b21\u6027\u5904\u7406\u6240\u6709\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u66f4\u590d\u6742\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>SELECT\n  l_content_id,\n  t_content_id,\n  pv\nFROM (\n  SELECT\n    data.*,\n#    \u6362\u8fd9\u884c  data.l_content_id = @last_data_id,\n    CASE\n      WHEN data.l_content_id = @last_data_id THEN @rank := @rank + 1\n      ELSE @rank := 1\n    END,\n    @rank AS r,\n    @last_data_id := data.l_content_id\n  FROM (\n    SELECT\n      l.content_id AS l_content_id,\n      --   l.uid_list\n      t.content_id AS t_content_id,\n      COUNT(t.uid) AS pv\n    FROM read_log AS t\n    JOIN (\n      SELECT\n        content_id,\n        GROUP_CONCAT(DISTINCT uid, '') AS uid_list\n      FROM read_log\n      GROUP BY\n        content_id\n    ) AS l\n      ON FIND_IN_SET(t.uid, l.uid_list)\n    GROUP BY\n      l.content_id,\n      t.content_id\n    ORDER BY\n      l.content_id,\n      pv DESC\n  ) AS data,\n  (SELECT @last_data_id := NULL, @rank := 0) AS tmp\n) AS a\nWHERE\n  r &lt; 4;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528 MySQL \u67e5\u8be2\u8ba1\u7b97\u51fa\u6bcf\u7bc7\u6587\u7ae0\u88ab\u54ea\u4e9b\u4eba\u6d4f\u89c8\u8fc7\uff0c\u5e76\u7edf\u8ba1\u8fd9\u4e9b\u6d4f\u89c8\u8005\u9605\u8bfb\u7684\u5176\u4ed6\u6587\u7ae0\u7684\u6d4f\u89c8\u6b21\u6570\uff0c\u5e76\u8f93\u51fa\u6d4f\u89c8\u6b21\u6570\u6700\u591a\u7684\u524d\u51e0\u7bc7\u6587\u7ae0\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>\u4e00\u4e2a sql \u8bf7\u6559\uff1a\u8ba1\u7b97\u5173\u8054\u5185\u5bb9\u7684\u6d4f\u89c8\u91cf \u95ee\u9898\uff1a \u5982\u4f55\u5229\u7528 \u67e5\u8be2\u8ba1\u7b97\u51fa\u6bcf\u7bc7\u6587\u7ae0\u88ab\u54ea\u4e9b\u4eba\u6d4f\u89c8\u8fc7\uff0c\u7136\u540e\u518d\u8ba1\u7b97\u51fa\u8fd9\u4e9b\u6d4f\u89c8\u8005\u66fe\u7ecf\u6d4f\u89c8\u8fc7\u7684\u5176\u4ed6\u6587\u7ae0\u7684\u6d4f\u89c8\u6b21\u6570\uff0c\u5e76\u8f93\u51fa\u6d4f\u89c8\u6b21\u6570\u6700\u591a\u7684\u524d\u51e0\u7bc7\u6587\u7ae0\uff1f sql \u793a\u4f8b\uff1a \u63d0\u4f9b\u7684\u793a\u4f8b sql \u53ea\u80fd\u8ba1\u7b97\u7279\u5b9a\u4e00\u7bc7\u5185\u5bb9\u7684\u6570\u636e\u3002\u95ee\u9898\u662f\uff0c\u6211\u4eec\u5e0c\u671b\u5bf9\u6bcf\u7bc7\u6587\u7ae0\u90fd\u8fdb\u884c\u540c\u6837\u7684\u8ba1\u7b97\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u65b9\u6cd5 1\uff08\u4e0d\u63a8\u8350\uff09 \u5982\u679c\u975e\u8981\u4f7f\u7528\u5355\u6761 sql \u8bed\u53e5\u89e3\u51b3\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4ee3\u7801\uff08\u672a\u7ecf\u6d4b\u8bd5\uff09\uff1a select l.content_id as l_content_id, t.content_id as t_content_id, count(t.uid) as pv from read_log as t join ( select content_id, group_concat(distinct uid, &#8221;) as uid_list from read_log group by content_id ) as l on find_in_set(t.uid, l.uid_list) group by l.content_id, t.content_id [&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-6743","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6743","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=6743"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6743\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}