{"id":628,"date":"2025-01-01T10:39:31","date_gmt":"2025-01-01T02:39:31","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=628"},"modified":"2025-01-01T10:39:31","modified_gmt":"2025-01-01T02:39:31","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e5%a4%a7%e5%9e%8b-mysql-%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad%e6%9c%89%e6%95%88%e5%88%a0%e9%99%a4%e9%87%8d%e5%a4%8d%e9%a1%b9%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/01\/%e5%a6%82%e4%bd%95%e5%9c%a8%e5%a4%a7%e5%9e%8b-mysql-%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad%e6%9c%89%e6%95%88%e5%88%a0%e9%99%a4%e9%87%8d%e5%a4%8d%e9%a1%b9%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728\u5927\u578b MySQL \u6570\u636e\u5e93\u4e2d\u6709\u6548\u5220\u9664\u91cd\u590d\u9879\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728\u5927\u578b MySQL \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879\u65f6\uff0c\u4f18\u5148\u8003\u8651\u7279\u5b9a\u6570\u636e\uff08\u5982\u4f18\u5148\u4fdd\u7559\u67d0\u4e9b\u8bb0\u5f55\uff09\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4e3a\u4e86\u786e\u4fdd\u9ad8\u6548\u5730\u5b8c\u6210\u8fd9\u9879\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u6d77\u91cf\u6570\u636e\u65f6\uff0c\u9700\u8981\u91c7\u53d6\u5408\u7406\u7684\u7b56\u7565\uff0c\u907f\u514d\u957f\u65f6\u95f4\u9501\u8868\u6216\u4ea7\u751f\u6027\u80fd\u95ee\u9898\u3002\u4e0b\u9762\u662f\u6709\u6548\u5220\u9664\u91cd\u590d\u9879\u7684\u6b65\u9aa4\u548c\u7b56\u7565\uff0c\u91cd\u70b9\u662f\u5982\u4f55\u4f18\u5148\u4fdd\u7559\u7279\u5b9a\u7684\u8bb0\u5f55\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u660e\u786e\u91cd\u590d\u6570\u636e\u7684\u5b9a\u4e49<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u5fc5\u987b\u660e\u786e\u54ea\u4e9b\u5b57\u6bb5\u88ab\u8ba4\u4e3a\u662f\u201c\u91cd\u590d\u201d\u7684\u3002\u5982\u679c\u4f60\u5df2\u7ecf\u77e5\u9053\u54ea\u4e9b\u5b57\u6bb5\u53ef\u80fd\u6709\u91cd\u590d\u6570\u636e\uff0c\u90a3\u4e48\u5220\u9664\u91cd\u590d\u9879\u7684\u64cd\u4f5c\u5c31\u53ef\u4ee5\u66f4\u76f4\u63a5\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u8868 <code>my_table<\/code>\uff0c\u5e76\u4e14\u8981\u5220\u9664 <code>column1<\/code> \u548c <code>column2<\/code> \u7684\u91cd\u590d\u8bb0\u5f55\uff0c\u4f46\u4f18\u5148\u4fdd\u7559 <code>column3<\/code> \u4e2d\u67d0\u4e9b\u7279\u5b9a\u7684\u503c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u5220\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u57fa\u672c\u7b56\u7565<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE my_table (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    column1 VARCHAR(255),\n    column2 VARCHAR(255),\n    column3 INT,\n    created_at DATETIME\n);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 1\uff1a\u67e5\u627e\u91cd\u590d\u9879<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u67e5\u627e\u54ea\u4e9b\u8bb0\u5f55\u662f\u91cd\u590d\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u67e5\u8be2\u627e\u51fa\u91cd\u590d\u7684\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, COUNT(*)\nFROM my_table\nGROUP BY column1, column2\nHAVING COUNT(*) &gt; 1;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u67e5\u8be2\u5c06\u8fd4\u56de\u6240\u6709\u91cd\u590d\u7684 <code>column1<\/code> \u548c <code>column2<\/code> \u7684\u7ec4\u5408\uff0c\u4ee5\u53ca\u6bcf\u4e2a\u7ec4\u5408\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 2\uff1a\u4fdd\u7559\u7279\u5b9a\u8bb0\u5f55\u5e76\u5220\u9664\u91cd\u590d\u8bb0\u5f55<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u5148\u8003\u8651\u4fdd\u7559\u7279\u5b9a\u6570\u636e<\/strong>\uff1a\u5047\u8bbe\u6211\u4eec\u5e0c\u671b\u6839\u636e <code>column3<\/code> \u7684\u67d0\u4e9b\u7279\u5b9a\u6761\u4ef6\u6765\u4fdd\u7559\u8bb0\u5f55\uff08\u4f8b\u5982\uff0c\u4fdd\u7559 <code>column3<\/code> \u503c\u4e3a\u6700\u5927\u7684\u8bb0\u5f55\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u4f7f\u7528\u5b50\u67e5\u8be2\u548c <code>JOIN<\/code> \u6765\u627e\u5230\u91cd\u590d\u7684\u8bb0\u5f55\uff0c\u5e76\u53ea\u4fdd\u7559\u67d0\u4e9b\u7279\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u5047\u8bbe\u6211\u4eec\u8981\u4fdd\u7559 <code>column3<\/code> \u6700\u5927\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE t1\nFROM my_table t1\nJOIN my_table t2\n    ON t1.column1 = t2.column1\n    AND t1.column2 = t2.column2\nWHERE t1.id &gt; t2.id\n    AND t1.column3 &lt; t2.column3;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7\u81ea\u8fde\u63a5 <code>JOIN<\/code> \u540c\u4e00\u5f20\u8868\uff0c\u67e5\u627e <code>column1<\/code> \u548c <code>column2<\/code> \u76f8\u540c\u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>t1.id > t2.id<\/code> \u9009\u62e9\u5220\u9664\u8f83\u65e9\u63d2\u5165\u7684\u8bb0\u5f55\uff08\u5373\u4fdd\u7559 <code>id<\/code> \u8f83\u5c0f\u7684\u8bb0\u5f55\uff09\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>t1.column3 &lt; t2.column3<\/code> \u6765\u786e\u4fdd\u4f18\u5148\u4fdd\u7559 <code>column3<\/code> \u6700\u5927\u7684\u8bb0\u5f55\uff08\u6216\u6309\u5176\u4ed6\u6761\u4ef6\u8fdb\u884c\u6392\u5e8f\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 <code>ROW_NUMBER()<\/code> \u6216 <code>RANK()<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e MySQL 8.0 \u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\uff08<code>ROW_NUMBER()<\/code>\uff09\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u91cd\u590d\u6570\u636e\u7684\u5220\u9664\uff0c\u540c\u65f6\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u4f18\u5148\u4fdd\u7559\u6570\u636e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 1\uff1a\u4f7f\u7528 <code>ROW_NUMBER()<\/code> \u6807\u8bb0\u6bcf\u4e2a\u91cd\u590d\u7ec4\u4e2d\u7684\u8bb0\u5f55<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528\u7a97\u53e3\u51fd\u6570 <code>ROW_NUMBER()<\/code> \u4e3a\u6bcf\u7ec4\u91cd\u590d\u6570\u636e\u751f\u6210\u4e00\u4e2a\u987a\u5e8f\u53f7\u3002\u4f8b\u5982\uff0c\u4fdd\u7559 <code>column3<\/code> \u6700\u5927\u7684\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>WITH ranked_data AS (\n    SELECT id, column1, column2, column3, \n           ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY column3 DESC) AS row_num\n    FROM my_table\n)\nSELECT id FROM ranked_data WHERE row_num &gt; 1;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u67e5\u8be2\u4f1a\u4e3a\u6bcf\u7ec4 <code>column1<\/code> \u548c <code>column2<\/code> \u7684\u91cd\u590d\u8bb0\u5f55\u6309\u7167 <code>column3<\/code> \u7684\u964d\u5e8f\u6392\u5e8f\uff0c\u5e76\u4e3a\u6bcf\u7ec4\u8bb0\u5f55\u5206\u914d\u4e00\u4e2a\u987a\u5e8f\u53f7\uff0c\u4fdd\u7559 <code>column3<\/code> \u6700\u5927\u7684\u8bb0\u5f55\uff08\u5373 <code>row_num = 1<\/code>\uff09\uff0c\u5176\u4f59\u7684\u8bb0\u5f55\u5c06\u5177\u6709 <code>row_num &gt; 1<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 2\uff1a\u5220\u9664\u91cd\u590d\u8bb0\u5f55<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u5220\u9664 <code>row_num &gt; 1<\/code> \u7684\u8bb0\u5f55\uff0c\u5373\u5220\u9664\u91cd\u590d\u9879\uff0c\u53ea\u4fdd\u7559\u4f18\u5148\u7684\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>WITH ranked_data AS (\n    SELECT id, column1, column2, column3, \n           ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY column3 DESC) AS row_num\n    FROM my_table\n)\nDELETE FROM my_table\nWHERE id IN (SELECT id FROM ranked_data WHERE row_num &gt; 1);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4f18\u5316\u5220\u9664\u64cd\u4f5c\u7684\u6027\u80fd<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u5904\u7406\u5927\u578b\u6570\u636e\u8868\u65f6\uff0c\u5220\u9664\u64cd\u4f5c\u53ef\u80fd\u4f1a\u975e\u5e38\u8017\u65f6\uff0c\u751a\u81f3\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>\u5206\u6279\u5220\u9664<\/strong>\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u76f4\u63a5\u5220\u9664\u6240\u6709\u91cd\u590d\u9879\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9501\u8868\u548c\u6027\u80fd\u74f6\u9888\u3002\u53ef\u4ee5\u5206\u6279\u5220\u9664\uff0c\u51cf\u5c11\u6bcf\u6b21\u64cd\u4f5c\u7684\u6570\u636e\u91cf\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>LIMIT<\/code> \u5206\u6279\u5220\u9664\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE t1\nFROM my_table t1\nJOIN my_table t2\n    ON t1.column1 = t2.column1\n    AND t1.column2 = t2.column2\nWHERE t1.id &gt; t2.id\n    AND t1.column3 &lt; t2.column3\nLIMIT 1000;  -- \u6bcf\u6b21\u5220\u9664 1000 \u6761<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u957f\u65f6\u95f4\u7684\u9501\u5b9a\uff0c\u786e\u4fdd\u5220\u9664\u64cd\u4f5c\u7684\u9ad8\u6548\u6027\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>\u521b\u5efa\u7d22\u5f15<\/strong>\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u786e\u4fdd\u4f60\u5728\u7528\u4e8e\u67e5\u627e\u91cd\u590d\u7684\u5b57\u6bb5\uff08\u5982 <code>column1<\/code> \u548c <code>column2<\/code>\uff09\u4e0a\u521b\u5efa\u4e86\u5408\u9002\u7684\u7d22\u5f15\uff0c\u8fd9\u6837\u53ef\u4ee5\u52a0\u901f\u67e5\u627e\u548c\u5220\u9664\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE INDEX idx_column1_column2 ON my_table (column1, column2);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>\u4f18\u5316\u67e5\u8be2<\/strong>\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c3d\u91cf\u907f\u514d\u4f7f\u7528 <code>JOIN<\/code> \u6216\u5b50\u67e5\u8be2\u4e2d\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u64cd\u4f5c\u3002\u5982\u679c\u5220\u9664\u6761\u4ef6\u590d\u6742\uff0c\u53ef\u4ee5\u8003\u8651\u9996\u5148\u5c06\u7b26\u5408\u5220\u9664\u6761\u4ef6\u7684\u8bb0\u5f55\u63d2\u5165\u4e34\u65f6\u8868\uff0c\u7136\u540e\u6279\u91cf\u5220\u9664\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5907\u4efd\u6570\u636e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u4e4b\u524d\uff0c\u59cb\u7ec8\u5efa\u8bae\u5907\u4efd\u6570\u636e\uff0c\u5c24\u5176\u662f\u5728\u6267\u884c\u5927\u89c4\u6a21\u5220\u9664\u64cd\u4f5c\u65f6\u3002\u53ef\u4ee5\u4f7f\u7528 <code>mysqldump<\/code> \u6216\u5176\u4ed6\u5907\u4efd\u5de5\u5177\u521b\u5efa\u6570\u636e\u5e93\u5feb\u7167\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqldump -u root -p my_database &gt; backup.sql<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5220\u9664\u91cd\u590d\u9879\u5e76\u4fdd\u7559\u4f18\u5148\u6570\u636e\u7684\u65b9\u6cd5\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u81ea\u8fde\u63a5\u5220\u9664\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>JOIN<\/code> \u548c <code>WHERE<\/code> \u5b50\u53e5\uff0c\u6839\u636e <code>id<\/code> \u6216\u5176\u4ed6\u5b57\u6bb5\u51b3\u5b9a\u4fdd\u7559\u54ea\u4e9b\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><strong>\u7a97\u53e3\u51fd\u6570\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>ROW_NUMBER()<\/code> \u6216 <code>RANK()<\/code> \u6807\u8bb0\u6bcf\u7ec4\u91cd\u590d\u8bb0\u5f55\uff0c\u5e76\u6839\u636e\u6392\u5e8f\u89c4\u5219\u9009\u62e9\u4fdd\u7559\u7684\u8bb0\u5f55\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e MySQL 8.0 \u53ca\u4ee5\u4e0a\u7248\u672c\uff0c<strong>\u4f7f\u7528\u7a97\u53e3\u51fd\u6570<\/strong> \u662f\u6700\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u66f4\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u5220\u9664\u64cd\u4f5c\u3002\u800c\u5bf9\u4e8e\u4f4e\u7248\u672c\u7684 MySQL\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u8fde\u63a5\u65b9\u6cd5\u5e76\u914d\u5408\u9002\u5f53\u7684\u7d22\u5f15\u6765\u63d0\u9ad8\u67e5\u8be2\u548c\u5220\u9664\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5927\u578b MySQL \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879\u65f6\uff0c\u4f18\u5148\u8003\u8651\u7279\u5b9a\u6570\u636e\uff08\u5982\u4f18\u5148\u4fdd\u7559\u67d0\u4e9b\u8bb0\u5f55\uff09\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4e3a\u4e86\u786e\u4fdd\u9ad8\u6548 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-628","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":629,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/628\/revisions\/629"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}