mysql把查询的结果保存到新表
有时我们要把查询的结果保存到新表里,创建新表,查询,插入显得十分麻烦。
其实直接可以搞定。例如把表2的查询结果插入表1:
如果表存在
1 | insert into tab1 select * from tab2 |
如果表不存在
1 | create table tab1 as select * from tab2 |
mysql将查询结果生成临时表
MySQL中将查询的结果生成临时表,列类型与查询的列一致,百度搜索到的没啥用。
直接上SQL:
将结果生成临时表
1 2 | create temporary table temp_tb_name as ( select id, name ,update_time from a_table where id |
手动创建临时表
1 2 3 4 5 6 | CREATE TEMPORARY TABLE temp_tb_bame( product_name VARCHAR (50) NOT NULL , total_sales DECIMAL (12,2) NOT NULL DEFAULT 0.00, avg_unit_price DECIMAL (7,2) NOT NULL DEFAULT 0.00, total_units_sold INT UNSIGNED NOT NULL DEFAULT 0 ); |
下面这种写法纯属误导:
直接创建物理表了emm~
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。