{"id":200755,"date":"2025-05-06T12:28:34","date_gmt":"2025-05-06T04:28:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200755\/"},"modified":"2025-05-06T12:28:34","modified_gmt":"2025-05-06T04:28:34","slug":"%e5%9c%a8mysql%e4%b8%ad%ef%bc%8cupdate-join%e8%af%ad%e5%8f%a5%e5%8f%af%e4%bb%a5%e5%8c%85%e5%90%aborder-by%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200755\/","title":{"rendered":"\u5728MySQL\u4e2d\uff0cUPDATE JOIN\u8bed\u53e5\u53ef\u4ee5\u5305\u542bORDER BY\u5417\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5728MySQL\u4e2d\uff0cUPDATE JOIN\u8bed\u53e5\u53ef\u4ee5\u5305\u542bORDER BY\u5417\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\u5728MySQL\u4e2d\uff0cUPDATE JOIN\u8bed\u53e5\u53ef\u4ee5\u5305\u542bORDER BY\u5417\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\/20241113\/1731485918673460de99c67.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728mysql\u4e2d\uff0c\u4f7f\u7528update join\u65f6\uff0c\u5e26\u6709order by\u4f1a\u5bfc\u81f4mysql\u5d29\u6e83\u5417\uff1f<\/strong><\/p>\n<p>\u5728mysql\u4e2d\uff0c\u4e0d\u80fd\u5728\u5e26\u6709join\u7684update\u8bed\u53e5\u4e2d\u4f7f\u7528order by\u3002\u8fd9\u4f1a\u5bfc\u81f4mysql\u5185\u90e8\u9519\u8bef\uff0c\u751a\u81f3\u5bfc\u81f4mysql\u5d29\u6e83\u3002<\/p>\n<p><strong>\u539f\u56e0<\/strong><\/p>\n<p>\u5f53update\u8bed\u53e5\u4e2d\u5305\u542bjoin\u65f6\uff0cmysql\u5c06\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u8bc4\u4f30join\u6761\u4ef6\uff0c\u5c06\u5339\u914d\u7684\u8bb0\u5f55\u5206\u7ec4\u3002<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u5339\u914d\u7684\u7ec4\uff0c\u6267\u884cupdate\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p>order by\u5b50\u53e5\u7528\u4e8e\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u5e8f\u3002\u4f46\u662f\uff0c\u5728\u5e26\u6709join\u7684update\u8bed\u53e5\u4e2d\uff0corder by\u65e0\u6cd5\u4fdd\u8bc1\u54ea\u4e2a\u7ec4\u5c06\u9996\u5148\u88ab\u66f4\u65b0\u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4mysql\u5185\u90e8\u9519\u8bef\u6216\u5d29\u6e83\u3002<\/p>\n<p><strong>\u4fee\u590d\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u4fee\u590d\u6b64\u95ee\u9898\uff0c\u5fc5\u987b\u4f7f\u7528\u5b50\u67e5\u8be2\u6216\u4e34\u65f6\u8868\u6765\u5bf9\u8bb0\u5f55\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<p><strong>\u4f7f\u7528\u5b50\u67e5\u8be2\uff1a<\/strong><\/p>\n<pre>update cfg_dev_network_push_status t1\nset ...\nwhere (t1.id, t1.push_status, t1.push_retry_times) in (\n    select id, push_status, push_retry_times\n    from (\n        select id, push_status, push_retry_times\n        from cfg_dev_network_push_status\n        join cfg_dev_network_bind_status on ...\n        order by push_status desc, push_retry_times desc, id asc\n    ) as subquery\n);<\/pre>\n<p><strong>\u4f7f\u7528\u4e34\u65f6\u8868\uff1a<\/strong><\/p>\n<pre>CREATE TEMPORARY TABLE tmp_sort AS\nSELECT id, push_status, push_retry_times\nFROM cfg_dev_network_push_status\nJOIN cfg_dev_network_bind_status ON ...\nORDER BY push_status DESC, push_retry_times DESC, id ASC;\n\nUPDATE cfg_dev_network_push_status t1\nSET ...\nWHERE (t1.id, t1.push_status, t1.push_retry_times) IN (\n    SELECT id, push_status, push_retry_times FROM tmp_sort\n);<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6570\u636e\u5e93\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5728MySQL\u4e2d\uff0cUPDATE J&#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-200755","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200755","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=200755"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200755\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}