UPDATE with join condition on matching words in columns of another table
The key here is that Column_1
, represents three possible values for the JOIN. So what you want to use is string_to_array()
(so long as those values are comma-separated and can not themselves include a comma).
Run this query,
SELECT id_number, string_to_array(column_1, ',') AS column_1
FROM table_a;
id_number | column_1
-----------+-----------------------
123 | {foo,bar,baz}
456 | {qux,quux,quuz}
789 | {corge,grault,garply}
101 | {qux,bar,grault}
Now, we can run our UPDATE
using = ANY()
,
UPDATE table_b
SET id_number = A.id_number
FROM (
SELECT id_number, string_to_array(column_1, ',') AS column_1
FROM table_a
) AS A
WHERE table_b.column_1 = ANY(A.column_1)
AND table_b.column_2 = ANY(A.column_1);
You can alternatively use <@
WHERE ARRAY[table_b.column_1, table_b.column_2] <@ A.column_1;
That even makes it a bit more compact..
UPDATE table_b
SET id_number = A.id_number
FROM table_a AS A
WHERE ARRAY[table_b.column_1, table_b.column_2] <@ string_to_array(A.column_1, ',');
Try this:
update table_b
set id_number = (select id_number
from table_a
where table_a.Column_1 like '%' || table_b.Column_1 || '%'
OR table_a.Column_1 like '%' || table_b.Column_2 || '%'
limit 1)
;
It can be another solutions by converting Column_1 into an array, but this is so clear.
Notice I'm limiting the search to 1 record, just in case the text appears in more than one column_1 of Table_A.
As Evan Carroll has pointed out in the comments sections, I'd remark that this code updates the whole table.
Check it here: http://rextester.com/MUL4593
Assuming Postgres 9.6, performance is relevant, big tables, "words" composed of characters, no whitespace or punctuation, no stemming or stop words, no phrases, all columns NOT NULL
.
Full Text search backed by an index should be among the fastest solutions:
UPDATE table_b b
SET id_number = a.id_number
FROM table_a a
WHERE to_tsvector('simple', a.column_1)
@@ plainto_tsquery('simple', concat_ws(' ', b.column_1, b.column_2))
AND b.id_number = a.id_number; -- prevent empty UPDATEs
With a matching expression index on a.column_1
:
CREATE INDEX table_a_column_1_idx ON table_a USING GIN (to_tsvector('simple', column_1));