{"id":243,"date":"2023-02-24T06:02:34","date_gmt":"2023-02-23T22:02:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=243"},"modified":"2023-04-30T15:20:13","modified_gmt":"2023-04-30T07:20:13","slug":"mysql-temporary-table","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/mysql-temporary-table\/","title":{"rendered":"MySQL \u4e34\u65f6\u8868"},"content":{"rendered":"<p>MySQL\u4e34\u65f6\u8868\u5728\u6211\u4eec\u9700\u8981\u4fdd\u5b58\u4e00\u4e9b\u4e34\u65f6\u6570\u636e\u65f6\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u4e34\u65f6\u8868\u53ea\u5728\u5f53\u524d\u8fde\u63a5\u53ef\u89c1\uff0c\u5f53\u5173\u95ed\u8fde\u63a5\u65f6\uff0cMysql\u4f1a\u81ea\u52a8\u5220\u9664\u8868\u5e76\u91ca\u653e\u6240\u6709\u7a7a\u95f4\u3002<\/p>\n<p>\u4e34\u65f6\u8868\u5728MySQL 3.23\u7248\u672c\u4e2d\u6dfb\u52a0\uff0c\u5982\u679cMySQL\u7248\u672c\u4f4e\u4e8e 3.23\u7248\u672c\u5c31\u65e0\u6cd5\u4f7f\u7528MySQL\u7684\u4e34\u65f6\u8868\u3002\u4e0d\u8fc7\u73b0\u5728\u4e00\u822c\u5f88\u5c11\u6709\u5728\u4f7f\u7528\u8fd9\u4e48\u4f4e\u7248\u672c\u7684MySQL\u6570\u636e\u5e93\u670d\u52a1\u3002<\/p>\n<p><!-- more --><\/p>\n<p>MySQL\u4e34\u65f6\u8868\u53ea\u5728\u5f53\u524d\u8fde\u63a5\u53ef\u89c1\uff0c\u5982\u679c\u4f7f\u7528PHP\u811a\u672c\u6765\u521b\u5efaMySQL\u4e34\u65f6\u8868\uff0c\u90a3\u6bcf\u5f53PHP\u811a\u672c\u6267\u884c\u5b8c\u6210\u540e\uff0c\u8be5\u4e34\u65f6\u8868\u4e5f\u4f1a\u81ea\u52a8\u9500\u6bc1\u3002<\/p>\n<p>\u5982\u679c\u4f7f\u7528\u5176\u4ed6MySQL\u5ba2\u6237\u7aef\u7a0b\u5e8f\u8fde\u63a5MySQL\u6570\u636e\u5e93\u670d\u52a1\u5668\u6765\u521b\u5efa\u4e34\u65f6\u8868\uff0c\u90a3\u4e48\u53ea\u6709\u5728\u5173\u95ed\u5ba2\u6237\u7aef\u7a0b\u5e8f\u65f6\u624d\u4f1a\u9500\u6bc1\u4e34\u65f6\u8868\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u624b\u52a8\u9500\u6bc1\u3002<\/p>\n<h2>\u521b\u5efaMySQL\u4e34\u65f6\u8868<\/h2>\n<p>\u4ee5\u4e0b\u5c55\u793a\u4e86\u4f7f\u7528MySQL\u4e34\u65f6\u8868\u7684\u7b80\u5355\u5b9e\u4f8b\uff0c\u4ee5\u4e0b\u7684SQL\u4ee3\u7801\u53ef\u4ee5\u9002\u7528\u4e8ePHP\u811a\u672c\u7684<code>mysql_query()<\/code>\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-sql\">mysql&gt; CREATE TEMPORARY TABLE SalesSummary (\n    -&gt; product_name VARCHAR(50) NOT NULL, \n    -&gt; total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00, \n    -&gt; avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00, \n    -&gt; total_units_sold INT UNSIGNED NOT NULL DEFAULT 0\n);\nQuery OK, 0 rows affected (0.00 sec)\n\nmysql&gt; INSERT INTO SalesSummary\n    -&gt; (product_name, total_sales, avg_unit_price, total_units_sold)\n    -&gt; VALUES\n    -&gt; (&#039;cucumber&#039;, 100.25, 90, 2);\n\nmysql&gt; SELECT * FROM SalesSummary;\n+--------------+-------------+----------------+------------------+\n| product_name | total_sales | avg_unit_price | total_units_sold |\n+--------------+-------------+----------------+------------------+\n| cucumber     |      100.25 |          90.00 |                2 |\n+--------------+-------------+----------------+------------------+\n1 row in set (0.00 sec)<\/code><\/pre>\n<p>\u5f53\u4f7f\u7528<code>SHOW TABLES<\/code>\u547d\u4ee4\u663e\u793a\u6570\u636e\u8868\u5217\u8868\u65f6\uff0c\u5c06\u65e0\u6cd5\u770b\u5230<code>SalesSummary<\/code>\u8868\u3002<\/p>\n<p>\u5982\u679c\u9000\u51fa\u5f53\u524dMySQL\u4f1a\u8bdd\uff0c\u518d\u4f7f\u7528<code>SELECT<\/code>\u547d\u4ee4\u6765\u8bfb\u53d6\u539f\u5148\u521b\u5efa\u7684\u4e34\u65f6\u8868\u6570\u636e\uff0c\u4f1a\u53d1\u73b0\u6570\u636e\u5e93\u4e2d\u6ca1\u6709\u8be5\u8868\u7684\u5b58\u5728\uff0c\u56e0\u4e3a\u5728\u9000\u51fa\u65f6\u8be5\u4e34\u65f6\u8868\u5df2\u7ecf\u88ab\u9500\u6bc1\u3002<\/p>\n<h2>\u5220\u9664MySQL\u4e34\u65f6\u8868<\/h2>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u65ad\u5f00\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u540e\uff0c\u4e34\u65f6\u8868\u5c31\u4f1a\u81ea\u52a8\u88ab\u9500\u6bc1\u3002\u5f53\u7136\u4e5f\u53ef\u4ee5\u5728\u5f53\u524dMySQL\u4f1a\u8bdd\u4f7f\u7528<code>DROP TABLE<\/code>\u547d\u4ee4\u6765\u624b\u52a8\u5220\u9664\u4e34\u65f6\u8868\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u624b\u52a8\u5220\u9664\u4e34\u65f6\u8868\u7684\u5b9e\u4f8b\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; CREATE TEMPORARY TABLE SalesSummary (\n    -&gt; product_name VARCHAR(50) NOT NULL, \n    -&gt; total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00, \n    -&gt; avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00, \n    -&gt; total_units_sold INT UNSIGNED NOT NULL DEFAULT 0\n);\nQuery OK, 0 rows affected (0.00 sec)\n\nmysql&gt; INSERT INTO SalesSummary\n    -&gt; (product_name, total_sales, avg_unit_price, total_units_sold)\n    -&gt; VALUES\n    -&gt; (&#039;cucumber&#039;, 100.25, 90, 2);\n\nmysql&gt; SELECT * FROM SalesSummary;\n+--------------+-------------+----------------+------------------+\n| product_name | total_sales | avg_unit_price | total_units_sold |\n+--------------+-------------+----------------+------------------+\n| cucumber     |      100.25 |          90.00 |                2 |\n+--------------+-------------+----------------+------------------+\n1 row in set (0.00 sec)\nmysql&gt; DROP TABLE SalesSummary;\nmysql&gt;  SELECT * FROM SalesSummary;\nERROR 1146: Table &#039;RUNOOB.SalesSummary&#039; doesn&#039;t exist<\/code><\/pre>\n<h2>\u514b\u9686\u521b\u5efa\u4e34\u65f6\u8868<\/h2>\n<p>\u7528\u67e5\u8be2\u76f4\u63a5\u521b\u5efa\u4e34\u65f6\u8868\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TEMPORARY TABLE \u4e34\u65f6\u8868\u540d AS\n(\n    SELECT *  FROM \u65e7\u7684\u8868\u540d\n    LIMIT 0,10000\n);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u4e34\u65f6\u8868\u5728\u6211\u4eec\u9700\u8981\u4fdd\u5b58\u4e00\u4e9b\u4e34\u65f6\u6570\u636e\u65f6\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u4e34\u65f6\u8868\u53ea\u5728\u5f53\u524d\u8fde\u63a5\u53ef\u89c1\uff0c\u5f53\u5173\u95ed\u8fde\u63a5\u65f6\uff0cMysql\u4f1a\u81ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-243","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}