{"id":6187,"date":"2024-11-14T13:20:44","date_gmt":"2024-11-14T05:20:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6187\/"},"modified":"2024-11-14T13:20:44","modified_gmt":"2024-11-14T05:20:44","slug":"mysql%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e8%a7%86%e5%9b%be","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6187\/","title":{"rendered":"mysql\u5982\u4f55\u521b\u5efa\u89c6\u56fe"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 mysql \u521b\u5efa\u89c6\u56fe\u7684\u6b65\u9aa4\uff1a\u6307\u5b9a\u89c6\u56fe\u540d\u79f0\uff08view_name\uff09\uff1b\u5b9a\u4e49\u67e5\u8be2\uff08as \u540e\u8ddf select \u8bed\u53e5\uff09\uff1b\u6267\u884c\u67e5\u8be2\uff08create view \u8bed\u53e5\uff09\u3002\u521b\u5efa\u540e\uff0c\u89c6\u56fe\u53ef\u50cf\u666e\u901a\u8868\u4e00\u6837\u4f7f\u7528\uff0c\u63d0\u4f9b\u7b80\u5316\u590d\u6742\u67e5\u8be2\u3001\u63d0\u9ad8\u6570\u636e\u5b89\u5168\u6027\u3001\u4f18\u5316\u6027\u80fd\u548c\u5b9e\u73b0\u6570\u636e\u865a\u62df\u5316\u7684\u4f18\u70b9\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/14\/2024061405303180162.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u521b\u5efa\u89c6\u56fe\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u521b\u5efa\u89c6\u56fe\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u521b\u5efa\u89c6\u56fe<\/strong><\/p>\n<p>\u89c6\u56fe\u662f\u57fa\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u8868\u521b\u5efa\u7684\u865a\u62df\u8868\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u590d\u6742\u67e5\u8be2\u7684\u7ed3\u679c\u4ee5\u7b80\u5355\u7684\u65b9\u5f0f\u5448\u73b0\u7ed9\u7528\u6237\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u521b\u5efa\u89c6\u56fe\u7684\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>CREATE VIEW view_name AS query_definition;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0b\u662f\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u6307\u5b9a\u89c6\u56fe\u540d\u79f0<\/strong><\/p>\n<p>\u4f7f\u7528 view_name \u6307\u5b9a\u521b\u5efa\u7684\u89c6\u56fe\u7684\u540d\u79f0\u3002<\/p>\n<p><strong>2. \u5b9a\u4e49\u67e5\u8be2<\/strong><\/p>\n<p>\u5728 AS \u5173\u952e\u5b57\u4e4b\u540e\uff0c\u7f16\u5199\u4e00\u4e2a\u67e5\u8be2\u5b9a\u4e49\u3002\u8be5\u67e5\u8be2\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u6709\u6548\u7684 SELECT \u8bed\u53e5\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a customer_orders \u7684\u89c6\u56fe\uff0c\u663e\u793a\u5ba2\u6237\u53ca\u5176\u8a02\u55ae\u4fe1\u606f\uff1a<\/p>\n<pre>CREATE VIEW customer_orders AS\nSELECT customers.name, orders.order_id, orders.amount\nFROM customers\nJOIN orders ON customers.id = orders.customer_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u6267\u884c\u67e5\u8be2<\/strong><\/p>\n<p>\u6267\u884c CREATE VIEW \u8bed\u53e5\u4ee5\u521b\u5efa\u89c6\u56fe\u3002<\/p>\n<p><strong>\u4f7f\u7528\u89c6\u56fe<\/strong><\/p>\n<p>\u521b\u5efa\u89c6\u56fe\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u5c31\u50cf\u4f7f\u7528\u666e\u901a\u8868\u4e00\u6837\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 SELECT \u8bed\u53e5\u67e5\u8be2\u89c6\u56fe\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b83\u5728\u5176\u4ed6\u67e5\u8be2\u6216\u8bed\u53e5\u4e2d\u3002<\/p>\n<p><strong>\u597d\u5904<\/strong><\/p>\n<p>\u4f7f\u7528\u89c6\u56fe\u7684\u597d\u5904\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>\u7b80\u5316\u590d\u6742\u67e5\u8be2\uff1a<\/strong>\u89c6\u56fe\u53ef\u4ee5\u5c06\u590d\u6742\u7684\u67e5\u8be2\u5305\u88c5\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u540d\u79f0\uff0c\u4fbf\u4e8e\u4f7f\u7528\u3002<\/li>\n<li> <strong>\u6570\u636e\u5b89\u5168\u6027\uff1a<\/strong>\u89c6\u56fe\u53ef\u4ee5\u7528\u6765\u9650\u5236\u5bf9\u5e95\u5c42\u8868\u6570\u636e\u7684\u8bbf\u95ee\uff0c\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/li>\n<li> <strong>\u6027\u80fd\u4f18\u5316\uff1a<\/strong>\u89c6\u56fe\u53ef\u4ee5\u9884\u5148\u8ba1\u7b97\u67e5\u8be2\u7ed3\u679c\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li> <strong>\u6570\u636e\u865a\u62df\u5316\uff1a<\/strong>\u89c6\u56fe\u53ef\u4ee5\u521b\u5efa\u6570\u636e\u62bd\u8c61\u5c42\uff0c\u7b80\u5316\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u521b\u5efa\u89c6\u56fe\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>\u4f7f\u7528 mysql \u521b\u5efa\u89c6\u56fe\u7684\u6b65\u9aa4\uff1a\u6307\u5b9a\u89c6\u56fe\u540d\u79f0\uff08view_name\uff09\uff1b\u5b9a\u4e49\u67e5\u8be2\uff08as \u540e\u8ddf select \u8bed\u53e5\uff09\uff1b\u6267\u884c\u67e5\u8be2\uff08create view \u8bed\u53e5\uff09\u3002\u521b\u5efa\u540e\uff0c\u89c6\u56fe\u53ef\u50cf\u666e\u901a\u8868\u4e00\u6837\u4f7f\u7528\uff0c\u63d0\u4f9b\u7b80\u5316\u590d\u6742\u67e5\u8be2\u3001\u63d0\u9ad8\u6570\u636e\u5b89\u5168\u6027\u3001\u4f18\u5316\u6027\u80fd\u548c\u5b9e\u73b0\u6570\u636e\u865a\u62df\u5316\u7684\u4f18\u70b9\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u521b\u5efa\u89c6\u56fe \u89c6\u56fe\u662f\u57fa\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u8868\u521b\u5efa\u7684\u865a\u62df\u8868\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u590d\u6742\u67e5\u8be2\u7684\u7ed3\u679c\u4ee5\u7b80\u5355\u7684\u65b9\u5f0f\u5448\u73b0\u7ed9\u7528\u6237\u7684\u65b9\u6cd5\u3002 \u521b\u5efa\u89c6\u56fe\u7684\u8bed\u6cd5\uff1a CREATE VIEW view_name AS query_definition; \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0b\u662f\u6b65\u9aa4\uff1a 1. \u6307\u5b9a\u89c6\u56fe\u540d\u79f0 \u4f7f\u7528 view_name \u6307\u5b9a\u521b\u5efa\u7684\u89c6\u56fe\u7684\u540d\u79f0\u3002 2. \u5b9a\u4e49\u67e5\u8be2 \u5728 AS \u5173\u952e\u5b57\u4e4b\u540e\uff0c\u7f16\u5199\u4e00\u4e2a\u67e5\u8be2\u5b9a\u4e49\u3002\u8be5\u67e5\u8be2\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u6709\u6548\u7684 SELECT \u8bed\u53e5\u3002 \u793a\u4f8b\uff1a \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a customer_orders \u7684\u89c6\u56fe\uff0c\u663e\u793a\u5ba2\u6237\u53ca\u5176\u8a02\u55ae\u4fe1\u606f\uff1a CREATE VIEW customer_orders AS SELECT customers.name, orders.order_id, orders.amount FROM customers JOIN orders ON customers.id = orders.customer_id; \u767b\u5f55\u540e\u590d\u5236 3. \u6267\u884c\u67e5\u8be2 [&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-6187","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6187","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=6187"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6187\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}