mysql查詢乙個表的資料插入另乙個表

2021-08-09 07:16:31 字數 672 閱讀 9262

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

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

例如: insert into t_a  select * from t_b ;

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

insert into 目標表 (欄位1, 欄位2, ...) select 欄位1, 欄位2, ... from **表;(相對應字段的型別必須保持一致)

例如:insert into t_a  ( name, age ) select name, age from t_b where age > 10;

或模糊匹配位數加一位 like '%310115132_%';

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

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

例如:insert into t_a (name) select name from t_b where not exists (select *  from t_a where t_a.name = t_b.name);

mysql從乙個表中查詢資料插入到另乙個表中

insert into table1 select from table2 insert into table1 field1 select field1 from table2 注意 select之後的字段如果大於乙個,不能用括號括起來,否則,系統會報錯 operand should contai...

Oracle查詢乙個表的資料插入到另乙個表

1.新增乙個表,通過另乙個表的結構和資料 create table xthame.tab1 as select from dsknow.combdversion 2.如果表存在 insert into tab1 select from tab2 3.同乙個表中,將a欄位的指賦給b欄位 update ...

Oracle查詢乙個表的資料插入到另乙個表

1.新增乙個表,通過另乙個表的結構和資料 create table xthame.tab1 as select from dsknow.combdversion 2.如果表存在 insert into tab1 select from tab2 3.同乙個表中,將a欄位的指賦給b欄位 update ...