[英]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
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.