根据新生成的第一个 table 的 ID 更新第二个 table 的列的查询

Query to update the column of second table based on newly generated id of first table

我有两个 tables company_types 和 companies_profiles。 company_types_id of company_types 和 company_type_id of companies_profiles 具有一对多关系。

最近为 platform_country_mapping_id bc82e358-919d-44f2-aa35-24416a052c21 添加了一些新条目到 company_types table 中,名称与我们之前为 8cb794b5- c37a-4c19-880d-355f65c1d33f。所以现在我必须用 company_types table.

中新生成的 company_types_id 更新 companies_profiles table 的 company_type_id 列

company_types

company_types_id    name                        platform_country_mapping_id     
1                   name1                       8cb794b5-c37a-4c19-880d-355f65c1d33f
2                   name2                       8cb794b5-c37a-4c19-880d-355f65c1d33f
3                   name3                       8cb794b5-c37a-4c19-880d-355f65c1d33f

101                 name1                       bc82e358-919d-44f2-aa35-24416a052c21
102                 name2                       bc82e358-919d-44f2-aa35-24416a052c21
103                 name3                       bc82e358-919d-44f2-aa35-24416a052c21

companies_profiles

company_id  company_type_id             platform_id                             platform_name
149         1                       8cb794b5-c37a-4c19-880d-355f65c1d33f        platformName1
150         2                       8cb794b5-c37a-4c19-880d-355f65c1d33f        platformName1
185         3                       8cb794b5-c37a-4c19-880d-355f65c1d33f        platformName1

248         1                       bc82e358-919d-44f2-aa35-24416a052c21        platformName2
249         2                       bc82e358-919d-44f2-aa35-24416a052c21        platformName2
250         1                       bc82e358-919d-44f2-aa35-24416a052c21        platformName2
251         3                       bc82e358-919d-44f2-aa35-24416a052c21        platformName2

需要更新的结果 companies_profiles table

company_id  company_type_id             platform_id                             platform_name
149         1                       8cb794b5-c37a-4c19-880d-355f65c1d33f        platformName1
150         2                       8cb794b5-c37a-4c19-880d-355f65c1d33f        platformName1
185         3                       8cb794b5-c37a-4c19-880d-355f65c1d33f        platformName1

248         101                     bc82e358-919d-44f2-aa35-24416a052c21        platformName2
249         102                     bc82e358-919d-44f2-aa35-24416a052c21        platformName2
250         101                     bc82e358-919d-44f2-aa35-24416a052c21        platformName2
251         103                     bc82e358-919d-44f2-aa35-24416a052c21        platformName2

下面是我尝试过的查询,并且至少对一个条目有效。

Update companies_profiles 
  set company_type_id= (Select company_types_id 
                        from company_types 
                        where name = (Select name 
                                      from company_types 
                                      where company_types_id = (Select company_type_id 
                                                                from companies_profiles 
                                                                where platform_name='platformName2' 
                                                                limit 1 offset 0)
                                     ) 
                          and platform_country_mapping_id='bc82e358-919d-44f2-aa35-24416a052c21'
                      ) 
where company_type_id = (Select company_type_id 
                         from companies_profiles 
                         where platform_name='platformName2' limit 1 offset 0) 
and platform_id='bc82e358-919d-44f2-aa35-24416a052c21'

无法对名称进行单一查询,因此我为每个名称编写了如下单独的查询:

Update companies_profiles set company_type_id=(Select company_types_id from company_types where name= 
'name1' and platform_country_mapping_id='bc82e358-919d-44f2-aa35-24416a052c21') where company_type_id=
(Select company_types_id from company_types where name= 
'name1' and platform_country_mapping_id='8cb794b5-c37a-4c19-880d-355f65c1d33f') 
and platform_id='bc82e358-919d-44f2-aa35-24416a052c21';

Update companies_profiles set company_type_id=(Select company_types_id from company_types where name= 
'name2' and platform_country_mapping_id='bc82e358-919d-44f2-aa35-24416a052c21') where company_type_id=
(Select company_types_id from company_types where name= 
'name2' and platform_country_mapping_id='8cb794b5-c37a-4c19-880d-355f65c1d33f') 
and platform_id='bc82e358-919d-44f2-aa35-24416a052c21';

Update companies_profiles set company_type_id=(Select company_types_id from company_types where name= 
'name3' and platform_country_mapping_id='bc82e358-919d-44f2-aa35-24416a052c21') where company_type_id=
(Select company_types_id from company_types where name= 
'name3' and platform_country_mapping_id='8cb794b5-c37a-4c19-880d-355f65c1d33f') 
and platform_id='bc82e358-919d-44f2-aa35-24416a052c21';