简体   繁体   English

如果列表包含表b另一列的子字符串,则用表b的列值更新列表

[英]update column table with value of column from table b if it contains substring from another column from table b

There are 2 table like below: 共有2个表格,如下所示:

Table a                        Table b

country_name                   id | string_bone   | country_name
------------------------       ---+---------------+---------------
usa                            1  | usa           | united states  
u s a                          2  | u s a         | united states
united states of america       3  | america       | united states
america                        4  | ...           | ...

I need to update table_a.country_name with table_b.country_name if table_b.string_bone is contained in table_a.country_name . 如果table_b.string_bone中包含table_a.country_name ,我需要使用table_b.country_name更新table_a.country_name

I tried this: 我尝试了这个:

UPDATE table_a a
SET country_name = b.country_name
WHERE EXISTS (SELECT country_name
              FROM table_b
              WHERE a.country_name LIKE '%' || string_bone || '%') b;

I would like table_a to look like this after the update: 我希望table_a在更新后看起来像这样:

Table a                        

country_name                   
------------------------       
united states                            
united states                          
united states      
united states                        

Here the dbfiddle link. 这里是dbfiddle链接。

OK, it's easy to implement this requirement, like below: 好的,很容易实现此要求,如下所示:

update table_a a set country_name = b.country_name from table_b b where a.country_name ~ b.country_name;

The data example as below: 数据示例如下:

postgres=# select * from table_a;
       country_name       
--------------------------
 usa
 u s a
 united states of america
 america
(4 rows)

postgres=# select * from table_b;
 country_name 
--------------
 america
 usa
 u s a
(3 rows)

postgres=# update table_a a set country_name = b.country_name from table_b b where a.country_name ~ b.country_name;
UPDATE 4
postgres=# select * from table_a;
 country_name 
--------------
 usa
 u s a
 america
 america
(4 rows)

Try the following: 请尝试以下操作:

UPDATE table_a a
    SET country_name = b.country_name
    from table_a t
    inner join table_b b
    on t.country_name LIKE '%' || b.string_bone || '%';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从表B到表A的MySQL更新列 - MySQL update column from table B to table A 如何使用来自表 B 列 3 Teradata 的输入更新表 A 列 3 - How to Update Table A column 3 with input from Table B column 3 Teradata 如何使用另一个表 B 中的值更新表 A 中的列,其中表 A 和 B 之间的关系为 1:N,使用 max() 函数 - How to update a column in a table A using the value from another table B wherein the relationship between tables A & B is 1:N by using max() function SQL触发器,用于将值从表A-列A复制到表B-列A IF值在表B中是唯一的 - SQL Trigger to copy value from Table A - Column A to Table B - Column A IF Value is unique in Table B SQL:将表 B 中的列添加到表 A - SQL: Adding column from table B to table A 如果表A中的列A与表B中的列B相等,则从两个表(表A和b)中获取输出 - get output from two tables(table A and b) if column A from table A is equal to column B from Table B 使用表B中的值访问表A的列值 - Accessing column value of table A using value from table B 从表中选择A列或B列中的值 - Select from table where value in column A or column B 如何根据表A中A列的结果更新表B中的b列 - How to update column b in table B based on results from column A in table A 从表 B 中删除,其中列值类似于带有通配符的表 a - Delete from table B where column value like table a with wildcards
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM