sql語句從一張表查詢欄位值插入另表中

2021-03-29 00:25:00 字數 4780 閱讀 4007

1樓:小丁創業

標準sql語句格式:

insert

into 表名(欄位名)

select 欄位名

from 表面

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

insert

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

from s,j,p

2樓:古舟蓑笠翁

insert into a(name,num,class) values('小米','1001',(select class from b where name = '小米'));

3樓:2一瞬間

4樓:匿名使用者

insert into a(產品,編號, 型別)

select 產品,編號, 型別 from b

where 產品=『小米』

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

5樓:明月照溝渠

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"g***data/g***data" -u"sa" -p""'

3、/** 匯入文字檔案

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

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

6樓:鬱筱羽

標準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

7樓:sql的藝術

insert into table2 (col1,col2,col3)

select col1,col2,col3 from table1

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

8樓:day忘不掉的痛

方法如下:

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

from 表1

where ...

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

9樓:育知同創教育

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

即可實現你所說的功能。

10樓:匿名使用者

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

insert into 表(select 條件 from 表)

11樓:

很簡單 就是一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中

12樓:尹巧駿

(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。

13樓:匿名使用者

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

from table_source

where ....

14樓:匿名使用者

insert into t1 value (select * from t2);

15樓:楊春三月

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

user_age   from  users ;

親測試可用!

內!容!

sql中如何把一個表中的某個欄位的值拿出來放在一起插入另一個表的一個欄位裡

16樓:匿名使用者

insert into 目標表(目標欄位) select 欄位1+'*'+欄位2+'*'+欄位3 from 源表

將一個表中的某個欄位插入到另一個表的欄位,如何寫sql語句?

17樓:漫奕琛寧媼

更改長度

ifexists(select

a.*from

syscolumns

ainner

join

sysobjectsbon

a.id=b.id

where

b.type

='u'

andb.name=upper('youtable')anda.name=lower('youfield'))alter

table

youtable

alter

column

youfield

char(60)

null

go新增

ifnot

exists(select

a.*from

syscolumns

ainner

join

sysobjectsbon

a.id=b.id

where

b.type='u'

andb.name=upper('youtable')anda.name=lower('youfield'))begin

alter

table

youtable

addyoufield

datetime

null

endgo

18樓:匿名使用者

樓主說的是更新吧,樓上說的是sql server的語法,不知道樓主是什麼資料庫,如果是oracle的話 建議這麼寫:

update a set col=(select col from b where a.id=b.id)

exists(select 1 from b where a.id=b.id )

注意:兩個表的id 一定要一一對應,不讓會報錯:查詢單個值返回多條記錄。

19樓:匿名使用者

注意:是插入還是更新?

插入的話:

insert into a(col) select col from b;

更新的話:

update a set col=select col from b where a.id=b.id;

20樓:江南煙夢

insert into table1(col1) select col2 from table2 where table1.id = table2.id

sql語句怎麼從一張表中查詢資料插入到另一張表中

首先,查詢出來的資料 式要與待插入的另一張表的格式相同,欄位型別相同。insert into b in d 另一張表所在文件 select from a where 上面就是把從a表中查詢到的資料插入到另一個文件中的b表中。insert into 表a 列1,列2 select 列1,列2 from...

關於sql查詢,想從很多表中查詢欄位值

select q.條碼 case when isnull a.a站點,then t else f end 是否經過a站點 case when isnull b.b站點,then t else f end 是否經過b站點 from 條碼錶 q left join a a on a.條碼 q.條碼lef...

怎麼用SQL語句同步兩個表中的欄位值

需要用觸發器 zhi來實現。如有兩張表dao a表和回b表,建立觸答發器使當a表插入資料後b表也同步插入資料。其中b表插入資料的欄位需要同a表中的欄位相對應。create trigger 觸發器名稱 on a表 after insert as begin insert into b表 b表欄位1,b...