程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql中select into 和sql中的select into 對比,mysqlselect

mysql中select into 和sql中的select into 對比,mysqlselect

編輯:MySQL綜合教程

mysql中select into 和sql中的select into 對比,mysqlselect


現在有張表為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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved