with tab3 as (with tab1 as (
select org_id,
org_name
from sys_org where
org_type = '1'
and org_code is not null
),
tab2 as (
select se.* from sys_enterprise se
left join sys_enterprise_org seo on seo.enterprise_id = se.enterprise_id
)
select tab1.org_id,tab1.org_name, tab2.enterprise_id, tab2.name
from tab1,tab2 where tab2.name like tab1.org_name || '%')
update sys_enterprise_org set org_id = tab3.org_id from tab3 where sys_enterprise_org.enterprise_id = tab3.enterprise_id;PostgreSQl 批量更新语句
于 2023-02-09 15:33:49 首次发布
该SQL语句首先从sys_org表中选择org_type为1且org_code非空的记录,然后通过LEFTJOIN连接sys_enterprise和sys_enterprise_org表。查询条件是tab2的name与tab1的org_name后跟%匹配。最后,更新sys_enterprise_org表中的org_id,使其等于匹配到的tab3.org_id。

被折叠的 条评论
为什么被折叠?



