用現有表往另一張表插資料

2022-03-06 12:12:35 字數 578 閱讀 8991

設有a表:create table a(int  id,varchar(10)  name, varchar(1) gender)

有b表:create table b(int  id,varchar(10)  name, varchar(1) gender, int money, int tax)

特點:b表結構是a表結構 多了幾個字段 

情景零:將a表完全複製(結構+資料)

create table c  as select *  from a

情景一:將 a 表中的資料全部複製到b表中,指定b表中字段即可。b中字段與a中不連續對應的情況亦可。

insert into b (id, name ,gender)  select *  from a;

情景二:將a 表中的資料全部複製到b表中,要求b中所以欄位都填滿

insert into b select a.*,100,2 from a a 

或insert into b select a.*,100,2 from a a  where a.gender='m'

這種情景可以擴充套件至,將多個表資料複製到同乙個表,多個錶用where關聯。

根據一張表去更新另一張表

最近在改乙個專案,由於是別人做好的,很多資料表資訊不全。不得不手工用sql更新資料表。現在又這麼2張表 第一張是管理員表 id 使用者id c id 分公司id p id 部門id name 使用者名稱 第二張是訂單表 id 訂單id com id 訂單所屬銷售的公司id dep id 訂單所屬銷售...

過濾另一張表

方法一 使用 not in,僅適用單個字段,效率低 select a.id from a where a.id not in select id from b 方法二 適用多個字段匹配,效率中 使用 left join.on.b.id isnull 表示左連線之後在b.id 欄位為 null的記錄 ...

從一張表資料匯入到另一張表

1 insert into select語句 語句形式為 insert into table2 field1,field2,select field1 field2 from table1 或者 insert into table2 select from table1 注意 1 要求目標表tabl...