MySQL select into 和 SQL select into

來源:互聯網
上載者:User

現在有張表為student,我想將這個表裡面的資料複製到一個為dust的新表中去,雖然可以用以下語句進行複製,總覺得不爽,希望各位協助下我,謝謝。
answer 01:
create table dust select * from student;//用於複製前未建立新表dust的情況下
answer 02:
insert into dust select * from student;//已經建立了新表dust的情況下

現在請各位用select..into..語句實現以上東東,謝謝支援,再次感謝!
網友回複:該回複於2008-09-29 06:37:03被版主刪除
網友回複:sqlserver可以那樣寫,但是mySql中貌似不行哦,另外樓主你說的那兩種寫法,應該是最常見的啊
網友回複:create table dust select * from student;
網友回複:樓上的寫的是我發的裡面的啊,我要的是 select into 語句的啊?

請各位大俠幫忙,謝謝!
網友回複:MySQL不支援Select Into語句直接備份表結構和資料,一些種方法可以代替, 也有其它方法可以處理,總結如下:
方法1:
MYSQL不支援:
Select * Into new_table_name from old_table_name;
替代方法:
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的後面!

網友回複:引用 5 樓 wufongming 的回複:
MySQL不支援Select Into語句直接備份表結構和資料,一些種方法可以代替, 也有其它方法可以處理,總結如下:
方法1:
MYSQL不支援:
Select * Into new_table_name from old_table_name;
替代方法:
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 okt…
 
5樓的朋友把上面樓主對在mysql中的select into語句的疑惑做了說明,在mysql中不支援Select Into語句直接備份表結構和資料。

我認為方法1還是比方法二靈活很多,因為這裡樓上朋友提出的解決方案是建立一個新的表,而且是表的欄位名和結構和原來的表都一樣,要是已經有一個新表被建立,只是表裡面沒有資料,而新表的欄位名和原表不同但類型相同,這時第二種方法就不適用了。

所以我認為方法一稍微好點,有修改的餘地,但5樓朋友能把問題說得這麼好已經很不容易了!

樓主根據自己的需要選擇吧!
網友回複:對了,樓主要是在SQL SERVER資料庫下用select into語句來實現表的資料的複製是個不錯的選擇!
網友回複:有收穫!
網友回複:感謝了,小弟佩服!
網友回複:引用 5 樓 wufongming 的回複:
MySQL不支援Select Into語句直接備份表結構和資料,一些種方法可以代替, 也有其它方法可以處理,總結如下:
方法1:
MYSQL不支援:
Select * Into new_table_name from old_table_name;
替代方法:
Create table new_table_name (Select * from old_table_name);

方法2:
1.先備份表結構和資料
#匯出命令 -u使用者名稱 -p密碼 -h主機IP地址 資料庫名 表名1 > 匯出檔案.sql
mysqldump -u…

 

 

 

補充一點,A表資料 複製到B表,B表不能有自增ID

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

insert into B (title) select title from A

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.