{"id":6707,"date":"2024-11-14T11:26:29","date_gmt":"2024-11-14T03:26:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6707\/"},"modified":"2024-11-14T11:26:29","modified_gmt":"2024-11-14T03:26:29","slug":"mysql-%e4%b8%ad%e4%bc%aa%e8%a1%a8-dual-%e7%9a%84%e4%b8%8d%e5%90%8c%e6%9f%a5%e8%af%a2%e6%96%b9%e5%bc%8f%e6%9c%89%e4%bd%95%e5%8c%ba%e5%88%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6707\/","title":{"rendered":"MySQL \u4e2d\u4f2a\u8868 dual \u7684\u4e0d\u540c\u67e5\u8be2\u65b9\u5f0f\u6709\u4f55\u533a\u522b\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172975307459564.jpg\" class=\"aligncenter\" title=\"MySQL \u4e2d\u4f2a\u8868 dual \u7684\u4e0d\u540c\u67e5\u8be2\u65b9\u5f0f\u6709\u4f55\u533a\u522b\uff1f\u63d2\u56fe\" alt=\"MySQL \u4e2d\u4f2a\u8868 dual \u7684\u4e0d\u540c\u67e5\u8be2\u65b9\u5f0f\u6709\u4f55\u533a\u522b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u63a2\u7d22 MySQL \u4e2d\u7684\u4f2a\u8868 dual<\/strong><\/p>\n<p><strong>\u4e0d\u540c\u7684\u67e5\u8be2\u65b9\u5f0f<\/strong><\/p>\n<p>\u95ee\u9898\u4e2d\u63d0\u5230\u4e24\u79cd\u67e5\u8be2\u65b9\u5f0f\uff0c\u4e00\u79cd\u4ece\u4f2a\u8868 dual \u4e2d\u67e5\u8be2\uff0c\u53e6\u4e00\u79cd\u76f4\u63a5\u67e5\u8be2\uff1a<\/p>\n<ul>\n<li>select 6+6 from dual;<\/li>\n<li>select 6+6;<\/li>\n<\/ul>\n<p><strong>\u5b83\u4eec\u4e4b\u95f4\u7684<\/strong><\/p>\n<p>\u7b54\u6848\u660e\u786e\u6307\u51fa\uff0c\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ed3\u679c\u662f\u6ca1\u6709\u533a\u522b\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u5728 MySQL \u4e2d\uff0cdual \u4f2a\u8868\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u4e34\u65f6\u8868\uff0c\u5b83\u53ea\u6709\u4e00\u884c\u4e00\u5217\uff0c\u5176\u503c\u4e3a NULL\u3002\u5f53\u4ece dual \u4e2d\u67e5\u8be2\u65f6\uff0cMySQL \u57fa\u672c\u4e0a\u5c06\u67e5\u8be2\u8f6c\u6362\u4e3a\u76f4\u63a5\u67e5\u8be2\u3002<\/p>\n<p><strong>MySQL \u4e2d\u4f2a\u8868\u7684\u5e38\u89c1\u573a\u666f<\/strong><\/p>\n<p>\u867d\u7136\u5728 MySQL \u4e2d\u4ece dual \u8868\u4e2d\u67e5\u8be2\u4e0e\u76f4\u63a5\u67e5\u8be2\u6ca1\u6709\u533a\u522b\uff0c\u4f46\u4f2a\u8868\u5728 Oracle \u7b49\u5176\u4ed6\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u5f88\u5e38\u7528\u3002\u5b83\u4eec\u7684\u5e38\u89c1\u573a\u666f\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>\u63d0\u4f9b\u7f3a\u7701\u503c\uff1a<\/strong>dual \u8868\u53ef\u4ee5\u7528\u4e8e\u5728\u67e5\u8be2\u4e2d\u63d0\u4f9b\u7f3a\u7701\u503c\uff0c\u4f8b\u5982 select * from table where field is null or field = (select field from dual) \u4e2d\u7684\u7f3a\u7701\u503c\u3002<\/li>\n<li> <strong>\u6a21\u62df\u591a\u884c\u8bb0\u5f55\uff1a<\/strong>\u4f7f\u7528\u7b1b\u5361\u5c14\u79ef\u6216 UNION \u53ef\u4ee5\u5c06\u6765\u81ea dual \u8868\u7684\u5355\u884c\u591a\u6b21\u56de\u6536\u6765\u6a21\u62df\u591a\u884c\u8bb0\u5f55\u3002\u4f8b\u5982\uff0cselect id, name from dual cross join (select id, name from table) \u4f1a\u4ea7\u751f table \u4e2d\u6bcf\u4e2a id \u548c name \u7684\u6240\u6709\u7ec4\u5408\u3002<\/li>\n<li> <strong>\u751f\u6210\u5e8f\u5217\uff1a<\/strong>\u901a\u8fc7\u4f7f\u7528 COUNT(*) \u548c UNION \u53ef\u4ee5\u4ece dual \u8868\u4e2d\u751f\u6210\u5e8f\u5217\u3002\u4f8b\u5982\uff0cselect count(*) from dual union all select count(*) from dual &#8230; \u4ea7\u751f\u4e00\u4e2a\u65e0\u7a77\u5e8f\u5217\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u4e2d\u4f2a\u8868 dual \u7684\u4e0d\u540c\u67e5\u8be2\u65b9\u5f0f\u6709\u4f55\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>\u63a2\u7d22 MySQL \u4e2d\u7684\u4f2a\u8868 dual \u4e0d\u540c\u7684\u67e5\u8be2\u65b9\u5f0f \u95ee\u9898\u4e2d\u63d0\u5230\u4e24\u79cd\u67e5\u8be2\u65b9\u5f0f\uff0c\u4e00\u79cd\u4ece\u4f2a\u8868 dual \u4e2d\u67e5\u8be2\uff0c\u53e6\u4e00\u79cd\u76f4\u63a5\u67e5\u8be2\uff1a select 6+6 from dual; select 6+6; \u5b83\u4eec\u4e4b\u95f4\u7684 \u7b54\u6848\u660e\u786e\u6307\u51fa\uff0c\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ed3\u679c\u662f\u6ca1\u6709\u533a\u522b\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u5728 MySQL \u4e2d\uff0cdual \u4f2a\u8868\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u4e34\u65f6\u8868\uff0c\u5b83\u53ea\u6709\u4e00\u884c\u4e00\u5217\uff0c\u5176\u503c\u4e3a NULL\u3002\u5f53\u4ece dual \u4e2d\u67e5\u8be2\u65f6\uff0cMySQL \u57fa\u672c\u4e0a\u5c06\u67e5\u8be2\u8f6c\u6362\u4e3a\u76f4\u63a5\u67e5\u8be2\u3002 MySQL \u4e2d\u4f2a\u8868\u7684\u5e38\u89c1\u573a\u666f \u867d\u7136\u5728 MySQL \u4e2d\u4ece dual \u8868\u4e2d\u67e5\u8be2\u4e0e\u76f4\u63a5\u67e5\u8be2\u6ca1\u6709\u533a\u522b\uff0c\u4f46\u4f2a\u8868\u5728 Oracle \u7b49\u5176\u4ed6\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u5f88\u5e38\u7528\u3002\u5b83\u4eec\u7684\u5e38\u89c1\u573a\u666f\u5305\u62ec\uff1a \u63d0\u4f9b\u7f3a\u7701\u503c\uff1adual \u8868\u53ef\u4ee5\u7528\u4e8e\u5728\u67e5\u8be2\u4e2d\u63d0\u4f9b\u7f3a\u7701\u503c\uff0c\u4f8b\u5982 select * from table where field is null or field = (select field from dual) \u4e2d\u7684\u7f3a\u7701\u503c\u3002 \u6a21\u62df\u591a\u884c\u8bb0\u5f55\uff1a\u4f7f\u7528\u7b1b\u5361\u5c14\u79ef\u6216 UNION \u53ef\u4ee5\u5c06\u6765\u81ea dual \u8868\u7684\u5355\u884c\u591a\u6b21\u56de\u6536\u6765\u6a21\u62df\u591a\u884c\u8bb0\u5f55\u3002\u4f8b\u5982\uff0cselect [&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-6707","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6707","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=6707"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6707\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}