相关推荐recommended
数据库将一张表中的数据更新到另一张表(Oracle、MySQL)
作者:mmseoamin日期:2023-12-14

Oracle数据库

        方式一(推荐)
-- 将 t2表的字段更新到t1表
MERGE INTO COMMUNITY_MEMBER_APPLY_DIE T1
USING COMMUNITY_MEMBER T2
ON (T1.MEMBER_ID = T2.MEMBER_ID)
WHEN MATCHED THEN
    UPDATE
    SET T1.REAL_NAME = T2.USER_NAME,
        T1.CARD_NUM=T2.CARDNUM
    where T1.CARD_NUM is null
       or T1.REAL_NAME is null;
        方式二
-- 将 t2表的字段更新到t1表
update COMMUNITY_MEMBER_APPLY_DIE T1
set (REAL_NAME, CARD_NUM)
        = (select T2.USER_NAME, T2.CARDNUM from COMMUNITY_MEMBER T2 where T1.MEMBER_ID = T2.MEMBER_ID)
where T1.CARD_NUM is null
   or T1.REAL_NAME is null;

        方式一比方式二效率快很多

MySQL数据库 

-- 将 t2表的字段更新到t1表
update org T1, community_area T2
set T1.prov=T2.province,
    T1.city=T2.city,
    T1.dist=T2.district,
    T1.street=T2.town,
    T1.community=T2.village
where T1.area_code = T2.area_code;