資料庫從一張表向另一張表怎麼插入資料

時間 2021-10-14 22:24:01

1樓:千鋒教育

下面以mysql資料庫為例分情況一一說明:

兩張表:inserttest和inserttest2,前者中有測試資料:

1.如果2張表的欄位一致,並且希望插入全部資料,可以用這種方法:

insert into 目標表 select * from **表;

insert into inserttest select * from inserttest2;

2.如果只希望匯入指定欄位,可以用這種方法:

insert into 目標表 (欄位1, 欄位2, ...) select 欄位1, 欄位2, ... from **表;

注意欄位的順序必須一致。

insert into inserttest2(id) select id from inserttest2;

3.如果您需要只匯入目標表中不存在的記錄,可以使用這種方法:

insert into 目標表

(欄位1, 欄位2, ...)

select 欄位1, 欄位2, ...

from **表

where not exists (select * from 目標表

where 目標表.比較欄位 = **表.比較欄位);

1>.插入多條記錄:

insert into inserttest2

(id,name)

select id,name

from inserttest

where not exists (select * from inserttest2

where inserttest2.id=inserttest.id);

2>.插入一條記錄:

insert into inserttest

(id, name)

select 100, 'liudehua'

from dual

where not exists (select * from inserttest

where inserttest.id = 100);

2樓:賓士

"insert into stocksale(cardid,pcode,pname,price,pnum) select" +cardid.text+"," +pcode.text+"," +pnum.

text+",pname,price,pnum from 表2"

---以上,希望對你有所幫助。

3樓:匿名使用者

以pcode為查詢條件,先從另一個表中將你要的資料取出,取放入你的insert 語句就可以了。

從資料結構 上看,你的2個表設計 有問題的

sql語句 怎麼把從一個表中查出來資料插入到另一個表中

4樓:明月照溝渠

1、假如

則 insert into a(a,b,c) (select a,b,c from b)

2、假如a表不存在

select a,b,c into a from b

3、假如需要跨資料庫

insert into adb.[dbo].a(a,b,c)  (select a,b,c from bdb.[dbo].b)

擴充套件資料:

sql匯入語句

1、如果要匯出資料到已經生成結構(即現存的)foxpro表中,可以直接用下面的sql語句

insert into openrowset('msdasql',

'driver=microsoft visual foxpro driver;sourcetype=dbf;sourcedb=c:\',

'select * from [aa.dbf]')

select * from 表

說明:sourcedb=c:\ 指定foxpro表所在的資料夾

aa.dbf 指定foxpro表的檔名.

2、匯出到excel

exec master..xp_cmdshell 'bcp settledb.dbo.

shanghu out c:\temp1.xls -c -q -s"gnetdata/gnetdata" -u"sa" -p""'

3、/** 匯入文字檔案

exec master..xp_cmdshell 'bcp dbname..tablename in c:

\dt.txt -c -sservername -usa -ppassword'

5樓:鬱筱羽

標準sql語句

bai格式:

insert

into 表名(

du欄位zhi

名)select 欄位名

from 表面

例子:dao將內查詢出的s表中容sno,j表中jno,p表中pno插入spj表中

insert

into spj(sno,jno,pno)select sno,jno,pno

from s,j,p

6樓:sql的藝術

insert into table2 (col1,col2,col3)

select col1,col2,col3 from table1

記得插入表的列數要與查詢結果列數一致,並且資料格式也需一致

7樓:day忘不掉的痛

方法如下:

insert into 表2(欄位名1,欄位名2,.....)select 欄位1,欄位2,...

from 表1

where ...

其中欄位型別必須完全符合。

8樓:育知同創教育

使用insert into 目標表(欄位列表) select 欄位列表 from 原始表

即可實現你所說的功能。

9樓:匿名使用者

你要查什麼資料據?算了,我這是巢狀語句,你看著往裡面換欄位就可以了

insert into 表(select 條件 from 表)

10樓:

很簡單 就是一bai個du

inert into table(col1,col2,…)select col1,col2,… 語句例如:insert into a(id,name) select id,name from emp;

表示zhi從emp表中查dao

詢出來的

id,name值專 插入到屬a表的id,name中

11樓:尹巧駿

(1).select * into desttbl from srctbl

(2).insert into desttbl(fld1, fld2) select fld1, 5 from srctbl

以上兩句都是將 srctbl 的資料插入到 desttbl,但兩句又有區別的:

第一句(select into from)要求目內標表(desttbl)不存在,因容為在插入時會自動建立。

第二句(insert into select from)要求目標表(desttbl)存在,由於目標表已經存在,所以我們除了插入源表(srctbl)的欄位外,還可以插入常量,如例中的:5。

12樓:匿名使用者

insert into table_dest(column1, column2...)select column1, column2...

from table_source

where ....

13樓:匿名使用者

insert into t1 value (select * from t2);

14樓:楊春三月

insert  into  users1(id,name,age)  select  users.user_id,users.user_name,users.

user_age   from  users ;

親測試可用!

內!容!

SQL資料庫,如何把一張表從資料庫中插入到另外資料庫?如何寫語句

如果兩個表結構完全一樣的,用insert into data2.table2 select from data1.table1 如果結構不一樣或者你要指定欄位,用insert into data2.table2 欄位1,欄位2,欄位 select 欄位j,欄位k,欄位m from data1.tab...

sql怎麼將一張表的欄位賦值給另一張表

插入資料insert into tbytz userid select userid from tbuser更新資料則在tbuser和tbytz兩個表要有一個關係。如tbuser.a1 tbytz.a2update tbytz set tbytz.userid select userid from ...

sql資料庫中建立檢視怎樣把一張表的列值轉換成另一張表的

首初珍餘啟 獲取檢視名稱樹懶已經實現了,下面就是獲取檢視中所有列的名稱了,其實也是用一句sql語句就可以實現了,有興趣也來試試吧 declare objid int,objname char 40 set objname 你的檢視名稱 建個函式把 create function getyqstrin...