{"id":7726,"date":"2024-11-14T08:39:47","date_gmt":"2024-11-14T00:39:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7726\/"},"modified":"2024-11-14T08:39:47","modified_gmt":"2024-11-14T00:39:47","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:\/\/fwq.ai\/blog\/7726\/","title":{"rendered":"\u5728MySQL\u4e2d\uff0cUPDATE JOIN\u8bed\u53e5\u53ef\u4ee5\u5305\u542bORDER BY\u5417\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173120077341541.jpg\" class=\"aligncenter\" title=\"\u5728MySQL\u4e2d\uff0cUPDATE JOIN\u8bed\u53e5\u53ef\u4ee5\u5305\u542bORDER BY\u5417\uff1f\u63d2\u56fe\" alt=\"\u5728MySQL\u4e2d\uff0cUPDATE JOIN\u8bed\u53e5\u53ef\u4ee5\u5305\u542bORDER BY\u5417\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728MySQL\u4e2d\uff0cUPDATE JOIN\u8bed\u53e5\u53ef\u4ee5\u5305\u542bORDER BY\u5417\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>\u5728\u4e2d\uff0c\u4f7f\u7528update join\u65f6\uff0c\u5e26\u6709order by\u4f1a\u5bfc\u81f4mysql\u5d29\u6e83\u5417\uff1f \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 \u539f\u56e0 \u5f53update\u8bed\u53e5\u4e2d\u5305\u542bjoin\u65f6\uff0cmysql\u5c06\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a \u8bc4\u4f30join\u6761\u4ef6\uff0c\u5c06\u5339\u914d\u7684\u8bb0\u5f55\u5206\u7ec4\u3002 \u5bf9\u4e8e\u6bcf\u4e2a\u5339\u914d\u7684\u7ec4\uff0c\u6267\u884cupdate\u64cd\u4f5c\u3002 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 \u4fee\u590d\u65b9\u6cd5 \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 \u4f7f\u7528\u5b50\u67e5\u8be2\uff1a update cfg_dev_network_push_status t1 set &#8230; where (t1.id, t1.push_status, t1.push_retry_times) in ( select id, push_status, push_retry_times from ( select id, push_status, push_retry_times from cfg_dev_network_push_status join cfg_dev_network_bind_status on &#8230; order by push_status desc, push_retry_times desc, id asc ) as subquery ); \u767b\u5f55\u540e\u590d\u5236 [&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-7726","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7726","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=7726"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7726\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}