码迷,mamicode.com
首页 > 数据库 > 详细

mysql中select into 和sql中的select into 对比

时间:2015-03-11 19:01:08      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

现在有张表为student,我想将这个表里面的数据复制到一个为dust的新表中去。
answer 01:
create table dust select * from student;//用于复制前未创建新表dust的情况下
answer 02:
insert into dust select * from student;//已经创建了新表dust的情况下

现在使用select..into..语句实现以上东东。


MySQL不支持Select Into语句直接备份表结构和数据,一些种方法可以代替, 也有其它方法可以处理,总结如下:
方法1:
MYSQL不支持:
Select * Into new_table_name from old_table_name; 这是sql server中的用法
替代方法:
Create table new_table_name (Select * from old_table_name);


方法2:
1.先备份表结构和数据
#导出命令 -u用户名 -p密码 -h主机IP地址 数据库名 表名1 > 导出文件.sql
mysqldump -uroot -proot -h192.168.0.88 ok_db oktable2 > ok_db.sql

2.修改备份表的名字
3.登录MySQL
4.选择数据库
5.执行: Source 备份表的路径 如:Source d:/ok_db.sql 回车即可。
6.完成.


MySQL Select into outfile用于导出指定的查询数据到文件如下:

1.导出表中所有数据到C盘根目录outfile.txt中如下:
Select * into outfile ‘c://outfile.txt‘ from test;


2.导出表中指定查询条件2005-06-08号的数据到C盘根目录outfile1.txt中如下:
Select * into outfile ‘c://outfile.txt‘ from test where beginDate=‘2008-06-08‘;


mysql> load data local infile "d:/gpsdata.txt" into table positiondata fields terminated by ‘;‘ (userid,latitude,longitude,altitude,speed,innerid,repo
rttime,status);


LOAD DATA [LOW_PRIORITY CONCURRENT] [LOCAL] INFILE ’file_name.txt’
[REPLACE IGNORE]
INTO TABLE tbl_name
[FIELDS
[TERMINATED BY ’string’]
[[OPTIONALLY] ENCLOSED BY ’char’]
[ESCAPED BY ’char’ ]
]
[LINES
[STARTING BY ’string’]
[TERMINATED BY ’string’]
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = eXPr,...)]

fields和lines在前面,(col_name_or_user_var,…)在后面 如果你使用的时候直接把要写的这些属性放在表名后面,这样是不正确的,一定要写到fields和lines的后面!

 

补充一点,A表数据 复制到B表,B表不能有自增ID

如果有自增ID,则不插入自增

insert into B (title) select title from A

mysql中select into 和sql中的select into 对比

标签:

原文地址:http://www.cnblogs.com/qiangua323/p/4330609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!