{"id":448,"date":"2010-06-15T22:46:55","date_gmt":"2010-06-15T22:46:55","guid":{"rendered":""},"modified":"2010-06-15T22:46:55","modified_gmt":"2010-06-15T22:46:55","slug":"448","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=448","title":{"rendered":"MySQL union \u8bed\u6cd5\u4ee3\u7801\u6f14\u793a"},"content":{"rendered":"<p>UNION \u7528\u4e8e\u628a\u6765\u81ea\u8bb8\u591aSelect\u8bed\u53e5\u7684\u7ed3\u679c\u7ec4\u5408\u5230\u4e00\u4e2a\u7ed3\u679c\u96c6\u5408\u4e2d\u3002 \uff08\u5982\u679c\u4f60\u8981\u5c06\u591a\u4e2a\u8868\u7684\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u5408\u5e76\u8f93\u51fa\u6bd4\u5982\u8bf4 \u7fa4\u7ec4\u6d88\u606f\u8ddf\u4e2a\u4eba\u6d88\u606f\u8868\u662f\u5206\u79bb\u7684\u4f46\u662f\u60f3\u4e00\u8d77\u63d0\u53d6\u51fa\u6765\u5e76\u663e\u793a\u7684\u8bdd\u5c31\u53ef\u4ee5\u5982\u6b64\u5904\u7406\u3002\u901a\u8fc7MySQLUNION\u8054\u5408\u67e5\u8be2\u51fa\u6765\u5373\u53ef\uff09<\/p>\n<p>\u5217\u4e8e\u6bcf\u4e2aSelect\u8bed\u53e5\u7684\u5bf9\u5e94\u4f4d\u7f6e\u7684\u88ab\u9009\u62e9\u7684\u5217\u5e94\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\uff08\u524d\u63d0\u6761\u4ef6\u662f\u4e24\u4e2aSelect\u51fa\u6765\u7684\u5217\u7c7b\u578b\u8981\u4fdd\u6301\u4e00\u6837\u7684\u624d\u884c\uff01\uff09\u3002\uff08\u4f8b\u5982\uff0c\u88ab\u7b2c\u4e00 \u4e2a\u8bed\u53e5\u9009\u62e9\u7684\u7b2c\u4e00\u5217\u5e94\u548c\u88ab\u5176\u5b83\u8bed\u53e5\u9009\u62e9\u7684\u7b2c\u4e00\u5217\u5177\u6709\u76f8\u540c\u7684\u7c7b\u578b\u3002\uff09\u5728\u7b2c\u4e00\u4e2aSelect\u8bed\u53e5\u4e2d\u88ab\u4f7f\u7528\u7684\u5217\u540d\u79f0\u4e5f\u88ab\u7528\u4e8e\u7ed3\u679c\u7684\u5217\u540d\u79f0\u3002<\/p>\n<p>Select\u8bed\u53e5\u4e3a\u5e38\u89c4\u7684\u9009\u62e9\u8bed\u53e5\uff0c\u4f46\u662f\u53d7\u5230\u5982\u4e0b\u7684\u9650\u5b9a\uff1a<\/p>\n<p>\u53ea\u6709\u6700\u540e\u4e00\u4e2aSelect\u8bed\u53e5\u53ef\u4ee5\u4f7f\u7528INTO OUTFILE\u3002<\/p>\n<p>HIGH_PRIORITY\u4e0d\u80fd\u4e0e\u4f5c\u4e3aUNION\u4e00\u90e8\u5206\u7684Select\u8bed\u53e5\u540c\u65f6\u4f7f\u7528\u3002\u5982\u679c\u60a8\u5bf9\u7b2c\u4e00\u4e2a Select\u6307\u5b9a\u4e86HIGH_PRIORITY\uff0c\u5219\u4e0d\u4f1a\u8d77\u4f5c\u7528\u3002\u5982\u679c\u60a8\u5bf9\u5176\u5b83\u540e\u7eed\u7684Select\u8bed\u53e5\u6307\u5b9aHIGH_PRIORITY\uff0c\u5219\u4f1a\u4ea7\u751f\u8bed\u6cd5\u9519 \u8bef\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5bf9UNION\u4e0d\u4f7f\u7528\u5173\u952e\u8bcdALL\uff0c\u5219\u6240\u6709\u8fd4\u56de\u7684\u884c\u90fd\u662f\u552f\u4e00\u7684\uff0c\u5982\u540c\u60a8\u5df2\u7ecf\u5bf9\u6574\u4e2a\u7ed3\u679c\u96c6\u5408\u4f7f\u7528\u4e86DISTINCT\u3002\u5982\u679c\u60a8\u6307\u5b9a\u4e86ALL\uff0c\u60a8\u4f1a\u4ece \u6240\u6709\u7528\u8fc7\u7684Select\u8bed\u53e5\u4e2d\u5f97\u5230\u6240\u6709\u5339\u914d\u7684\u884c\u3002<\/p>\n<p>DISTINCT\u5173\u952e\u8bcd\u662f\u4e00\u4e2a\u81ea\u9009\u8bcd\uff0c\u4e0d\u8d77\u4efb\u4f55\u4f5c\u7528\uff0c\u4f46\u662f\u6839\u636eSQL\u6807\u51c6\u7684\u8981\u6c42\uff0c\u5728\u8bed\u6cd5\u4e2d\u5141\u8bb8\u91c7\u7528\u3002\uff08\u5728MySQL\u4e2d\uff0cDISTINCT\u4ee3\u8868\u4e00\u4e2a\u5171 \u7528\u4f53\u7684\u9ed8\u8ba4\u5de5\u4f5c\u6027\u8d28\u3002\uff09<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728\u540c\u4e00\u67e5\u8be2\u4e2d\u6df7\u5408UNION ALL\u548cUNION DISTINCT\u3002\u88ab\u6df7\u5408\u7684UNION\u7c7b\u578b\u6309\u7167\u8fd9\u6837\u7684\u65b9\u5f0f\u5bf9\u5f85\uff0c\u5373DISTICT\u5171\u7528\u4f53\u8986\u76d6\u4f4d\u4e8e\u5176\u5de6\u8fb9\u7684\u6240\u6709ALL\u5171\u7528\u4f53\u3002DISTINCT\u5171\u7528\u4f53\u53ef\u4ee5\u4f7f \u7528UNION DISTINCT\u660e\u786e\u5730\u751f\u6210\uff0c\u6216\u4f7f\u7528UNION\uff08\u540e\u9762\u4e0d\u52a0DISTINCT\u6216ALL\u5173\u952e\u8bcd\uff09\u9690\u542b\u5730\u751f\u6210\u3002<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u4f7f\u7528ORDER BY\u6216LIMIT\u5b50\u53e5\u6765\u5bf9\u5168\u90e8UNION\u7ed3\u679c\u8fdb\u884c\u5206\u7c7b\u6216\u9650\u5236\uff0c\u5219\u5e94\u5bf9\u5355\u4e2a\u5730Select\u8bed\u53e5\u52a0\u5706\u62ec\u53f7\uff0c\u5e76\u628aORDER BY\u6216LIMIT\u653e\u5230\u6700\u540e\u4e00\u4e2a\u7684\u540e\u9762\u3002\u4ee5\u4e0b\u4f8b\u5b50\u540c\u65f6\u4f7f\u7528\u4e86\u8fd9\u4e24\u4e2a\u5b50\u53e5\uff1a<\/p>\n<p>\u4ee3\u7801 \u590d\u5236\u4ee3\u7801<\/p>\n<p>(Select a FROM tbl_name Where a=10 AND B=1)UNION(Select a FROM tbl_name Where a=11 AND B=2)ORDER BY a LIMIT 10;&nbsp;&nbsp;<br \/>\uff08\u5982\u679c\u60f3\u8981\u5b9e\u73b0\u5206\u9875\u7684\u8bdd\u53ef\u4ee5\u8fd9\u6837\u5904\u7406 \u5c06\u4e24\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\u5f53\u4f5c\u662f\u4e00\u4e2a\u5927\u7684\u7ed3\u679c\u96c6\u5904\u7406\u7136\u540e\u518d\u5bf9\u6b64\u5927\u7684\u7ed3\u679c\u96c6\u8fdb\u884cLIMIT\u5904\u7406\u5373\u53ef\u5b9e\u73b0\uff01\uff09\u597d\u597d\u597d \uff5e\uff01<br \/>(Select a FROM tbl_name Where a=10 AND B=1)MySQLUNION(Select a FROM tbl_name Where a=11 AND B=2)ORDER BY a LIMIT 10;<\/p>\n<p>\u8fd9\u79cdORDER BY\u4e0d\u80fd\u4f7f\u7528\u5305\u62ec\u8868\u540d\u79f0\uff08\u4e5f\u5c31\u662f\uff0c\u91c7\u7528tbl_name.col_name\u683c\u5f0f\u7684\u540d\u79f0\uff09\u5217\u5f15\u7528\u3002\u53ef\u4ee5\u5728\u7b2c\u4e00\u4e2aSelect\u8bed\u53e5\u4e2d\u63d0\u4f9b\u4e00\u4e2a\u5217\u522b\u540d\uff0c\u5e76\u5728 orDER BY\u4e2d\u53c2\u9605\u522b\u540d\uff0c\u6216\u4f7f\u7528\u5217\u4f4d\u7f6e\u5728ORDER BY\u4e2d\u53c2\u9605\u5217\u3002\uff08\u9996\u9009\u91c7\u7528\u522b\u540d\uff0c\u56e0\u4e3a\u4e0d\u5efa\u8bae\u4f7f\u7528\u5217\u4f4d\u7f6e\u3002\uff09<\/p>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u5e26\u5206\u7c7b\u7684\u4e00\u5217\u6709\u522b\u540d\uff0c\u5219ORDER BY\u5b50\u53e5\u5fc5\u987b\u5f15\u7528\u522b\u540d\uff0c\u800c\u4e0d\u80fd\u5f15\u7528\u5217\u540d\u79f0\u3002\u4ee5\u4e0b\u8bed\u53e5\u4e2d\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u5fc5\u987b\u8fd0\u884c\uff0c\u4f46\u662f\u7b2c\u4e8c\u4e2a\u4f1a\u8fd0\u884c\u5931\u8d25\uff0c\u51fa\u73b0\u5728&#8217;order clause&#8217;\u4e2d\u6709\u672a\u77e5\u5217&#8217;a&#8217;\u7684\u9519\u8bef\uff1a<\/p>\n<p>\u4ee3\u7801 \u590d\u5236\u4ee3\u7801<\/p>\n<p>(Select a AS b FROM t) UNION (Select &#8230;) orDER BY b;(Select a AS b FROM t) UNION (Select &#8230;) orDER BY a;To apply orDER BY or LIMIT to an individual Select, place the clause inside the parentheses that enclose the Select:&nbsp;&nbsp; (Select a AS b FROM t) UNION (Select &#8230;) orDER BY b;(Select a AS b FROM t) UNION (Select &#8230;) orDER BY a;To apply orDER BY or LIMIT to an individual Select, place the clause inside the parentheses that enclose the Select:&nbsp;&nbsp;<br \/>\u4e3a\u4e86\u5bf9\u5355\u4e2aSelect\u4f7f\u7528ORDER BY\u6216LIMIT\uff0c\u5e94\u628a\u5b50\u53e5\u653e\u5165\u5706\u62ec\u53f7\u4e2d\u3002\u5706\u62ec\u53f7\u5305\u542b\u4e86Select\uff1a<\/p>\n<p>\u4ee3\u7801 \u590d\u5236\u4ee3\u7801<\/p>\n<p>(Select a FROM tbl_name Where a=10 AND B=1 orDER BY a LIMIT 10)UNION(Select a FROM tbl_name Where a=11 AND B=2 orDER BY a LIMIT 10);&nbsp;&nbsp; (Select a FROM tbl_name Where a=10 AND B=1 orDER BY a LIMIT 10)UNION(Select a FROM tbl_name Where a=11 AND B=2 orDER BY a LIMIT 10); <br \/>\u4e8c \u5b9e\u4f8b\u6269\u5c55 <\/p>\n<p>MySQLunion\u53ef\u4ee5\u5bf9\u540c\u4e00\u4e2a\u8868\u7684\u4e24\u6b21\u67e5\u8be2\u8054\u5408\u8d77\u6765. \u8fd9\u6837\u505a\u7684\u76ca\u5904\u4e5f\u975e\u5e38\u660e\u663e, \u6bd4\u5982\u5728blog\u5e94\u7528\u4e2d, \u53ef\u4ee5\u5229\u7528\u4e00\u6761sql\u8bed\u53e5\u5b9e\u73b0\u7f6e\u9876blog\u548c\u666e\u901ablog\u7684\u5206\u9875\u663e\u793a.<\/p>\n<p>\u4ee3\u7801 \u590d\u5236\u4ee3\u7801<\/p>\n<p>(&nbsp;&nbsp; Select *&nbsp;&nbsp; FROM `blog`&nbsp;&nbsp; Where top=1&nbsp;&nbsp; orDER BY created DESC&nbsp;&nbsp; )&nbsp;&nbsp; UNION (&nbsp;&nbsp; Select *&nbsp;&nbsp; FROM `blog`&nbsp;&nbsp; Where top = 0&nbsp;&nbsp; orDER BY created DESC&nbsp;&nbsp; ) LIMIT 2 , 3&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\u4ee5\u4e0a\u7684\u76f8\u5173\u5185\u5bb9\u5c31\u662f\u5bf9MySQLunion\u8bed\u6cd5\u7684\u4ecb\u7ecd<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UNION \u7528\u4e8e\u628a\u6765\u81ea\u8bb8\u591aSelect\u8bed\u53e5\u7684\u7ed3\u679c\u7ec4\u5408\u5230\u4e00\u4e2a\u7ed3\u679c\u96c6\u5408\u4e2d&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-448","post","type-post","status-publish","format-standard","hentry","category-DB"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=448"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/448\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}