{"id":3753,"date":"2025-05-29T00:04:06","date_gmt":"2025-05-28T16:04:06","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3753"},"modified":"2025-05-29T00:04:06","modified_gmt":"2025-05-28T16:04:06","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88mysql-%e8%a7%86%e5%9b%be%ef%bc%88view%ef%bc%89%e4%b8%ad%e4%b8%8d%e8%83%bd%e7%9b%b4%e6%8e%a5%e4%bd%bf%e7%94%a8%e4%b8%b4%e6%97%b6%e8%a1%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/05\/29\/%e4%b8%ba%e4%bb%80%e4%b9%88mysql-%e8%a7%86%e5%9b%be%ef%bc%88view%ef%bc%89%e4%b8%ad%e4%b8%8d%e8%83%bd%e7%9b%b4%e6%8e%a5%e4%bd%bf%e7%94%a8%e4%b8%b4%e6%97%b6%e8%a1%a8%ef%bc%9f\/","title":{"rendered":"\u4e3a\u4ec0\u4e48MySQL \u89c6\u56fe\uff08View\uff09\u4e2d\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\u4e34\u65f6\u8868\uff1f"},"content":{"rendered":"\n<p>\u5728 <strong>MySQL \u89c6\u56fe\uff08View\uff09\u4e2d<\/strong>\uff0c\u662f<strong>\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\u4e34\u65f6\u8868\uff08Temporary Table\uff09<\/strong>\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u89e3\u91ca\u4e0e\u66ff\u4ee3\u65b9\u6848\u5efa\u8bae\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4e3a\u4ec0\u4e48\u89c6\u56fe\u4e2d\u4e0d\u80fd\u4f7f\u7528\u4e34\u65f6\u8868\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f60\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89c6\u56fe\u662f\u4e00\u4e2a\u4fdd\u5b58\u7684 SQL \u67e5\u8be2\u5b9a\u4e49<\/strong>\uff08\u4e0d\u5b58\u6570\u636e\uff09\uff0c\u6bcf\u6b21\u4f7f\u7528\u65f6\u4f1a\u5b9e\u65f6\u5c55\u5f00\u6267\u884c\u3002<\/li>\n\n\n\n<li>\u4e34\u65f6\u8868\uff08<code>CREATE TEMPORARY TABLE<\/code>\uff09<strong>\u53ea\u5728\u5f53\u524d\u4f1a\u8bdd\u4e2d\u5b58\u5728<\/strong>\uff0c\u800c\u89c6\u56fe\u901a\u5e38\u88ab\u591a\u4e2a\u4f1a\u8bdd\u5171\u4eab\u3002<\/li>\n\n\n\n<li>\u6240\u4ee5\uff1a<strong>MySQL \u7981\u6b62\u89c6\u56fe\u5f15\u7528\u4e34\u65f6\u8868<\/strong>\uff0c\u4ee5\u907f\u514d\u8fd0\u884c\u65f6\u5f15\u7528\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b98\u65b9\u660e\u786e\u9650\u5236\uff1a<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>MySQL does not support <em>references to TEMPORARY tables within views<\/em>.<\/p>\n<\/blockquote>\n\n\n\n<p>\ud83d\udcda \u6765\u6e90\uff1a<br>\ud83d\udc49 <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/create-view.html\">MySQL \u5b98\u65b9\u6587\u6863\uff1aCREATE VIEW Restrictions<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u9519\u8bef\u7528\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u521b\u5efa\u4e34\u65f6\u8868\nCREATE TEMPORARY TABLE temp_orders AS\nSELECT * FROM orders WHERE status = 'pending';\n\n-- \u8bd5\u56fe\u57fa\u4e8e\u4e34\u65f6\u8868\u521b\u5efa\u89c6\u56fe\uff08\u9519\u8bef\uff09\nCREATE VIEW pending_view AS\nSELECT * FROM temp_orders;<\/code><\/pre>\n\n\n\n<p>\ud83d\uded1 \u9519\u8bef\u63d0\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ERROR 1349 (HY000): View's SELECT contains a temporary table<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u66ff\u4ee3\u65b9\u6848\uff1a\u5b9e\u73b0\u76f8\u540c\u76ee\u6807\u7684\u505a\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 1\uff1a\u4f7f\u7528\u5b50\u67e5\u8be2\u4ee3\u66ff\u4e34\u65f6\u8868<\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5c06\u4e34\u65f6\u8868\u7684\u67e5\u8be2\u903b\u8f91\u76f4\u63a5\u5185\u8054\u8fdb\u89c6\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE VIEW pending_view AS\nSELECT * FROM (\n    SELECT * FROM orders WHERE status = 'pending'\n) AS temp_orders_alias;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u4e0d\u7528\u4e34\u65f6\u8868\uff0c\u67e5\u8be2\u903b\u8f91\u4ecd\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 2\uff1a\u7528\u6301\u4e45\u5316\u8868 + \u81ea\u52a8\u6e05\u7406\u673a\u5236\u6a21\u62df<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60<strong>\u786e\u5b9e\u9700\u8981\u591a\u6b65\u5904\u7406\u4e34\u65f6\u6570\u636e<\/strong>\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<strong>\u666e\u901a\u8868 + \u6dfb\u52a0\u7528\u6237\u6807\u8bc6 + \u5b9a\u671f\u6e05\u7406<\/strong>\u6765\u6a21\u62df\u4e34\u65f6\u8868\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6a21\u62df\u4e34\u65f6\u8868\u7684\u521b\u5efa\nCREATE TABLE temp_orders_sim (\n    id INT,\n    user_id INT,\n    session_id VARCHAR(255),\n    created_at DATETIME\n);\n\n-- \u63d2\u5165\u6570\u636e\u65f6\u9644\u5e26\u5f53\u524d\u7528\u6237\/\u4f1a\u8bdd\u4fe1\u606f\nINSERT INTO temp_orders_sim (id, user_id, session_id, created_at)\nSELECT id, 123, 'session_abc', NOW() FROM orders WHERE status = 'pending';\n\n-- \u521b\u5efa\u89c6\u56fe\u57fa\u4e8e\u8be5\u8868\nCREATE VIEW pending_orders_view AS\nSELECT * FROM temp_orders_sim WHERE session_id = 'session_abc';<\/code><\/pre>\n\n\n\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u5f00\u53d1\u5904\u7406<strong>session \u7ba1\u7406\u548c\u6e05\u7406\u7b56\u7565<\/strong>\uff0c\u9002\u7528\u4e8e\u590d\u6742\u591a\u6b65\u67e5\u8be2\u7684\u670d\u52a1\u578b\u67b6\u6784\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u53ef\u4ee5\u5728\u5b58\u50a8\u8fc7\u7a0b\u4f7f\u7528\u4e34\u65f6\u8868<\/h2>\n\n\n\n<p>\u867d\u7136\u89c6\u56fe\u4e0d\u80fd\u7528\u4e34\u65f6\u8868\uff0c\u4f46\u4f60\u53ef\u4ee5\u5728 <strong>\u5b58\u50a8\u8fc7\u7a0b<\/strong>\u4e2d\u4f7f\u7528\u4e34\u65f6\u8868\u6765\u5c01\u88c5\u590d\u6742\u903b\u8f91\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELIMITER $$\n\nCREATE PROCEDURE get_pending_orders()\nBEGIN\n    CREATE TEMPORARY TABLE temp_orders AS\n    SELECT * FROM orders WHERE status = 'pending';\n\n    SELECT * FROM temp_orders;\n\n    DROP TEMPORARY TABLE IF EXISTS temp_orders;\nEND$$\n\nDELIMITER ;\n\n-- \u8c03\u7528\u5b58\u50a8\u8fc7\u7a0b\nCALL get_pending_orders();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u53ef\u884c\u6027<\/th><th>\u5efa\u8bae\u65b9\u6848<\/th><\/tr><\/thead><tbody><tr><td>\u5728\u89c6\u56fe\u4e2d\u4f7f\u7528\u4e34\u65f6\u8868<\/td><td>\u274c \u4e0d\u53ef<\/td><td>\u5b50\u67e5\u8be2\u6216\u6301\u4e45\u5316\u8868<\/td><\/tr><tr><td>\u5728\u89c6\u56fe\u4e2d\u4f7f\u7528\u5b50\u67e5\u8be2<\/td><td>\u2705 \u53ef\u884c<\/td><td>\u2705 \u63a8\u8350<\/td><\/tr><tr><td>\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u4e34\u65f6\u8868<\/td><td>\u2705 \u53ef\u884c<\/td><td>\u2705 \u63a8\u8350<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5ef6\u4f38\u9605\u8bfb<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/create-view.html\">MySQL CREATE VIEW \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/temporary-table-problems.html\">MySQL \u4e34\u65f6\u8868\u5b98\u65b9\u6587\u6863<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MySQL \u89c6\u56fe\uff08View\uff09\u4e2d\uff0c\u662f\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\u4e34\u65f6\u8868\uff08Temporary Table\uff09\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u89e3\u91ca\u4e0e\u66ff [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3754,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-3753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3753"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3753\/revisions"}],"predecessor-version":[{"id":3755,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3753\/revisions\/3755"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3754"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}