1

I have a column in table1 that contains names separated with commas, like a,b,c

names result
a,d,e
a,c,e,f
c,d,f,g

Another column with a single name in table2, like a or b or c

line name origin
1 a US
2 b UK
3 c UK
4 d AUS
5 e CAN
6 f UK
7 g UK

And I want to update table1.result if any names from table1.names are in table2.name & origin = UK.

Tried this, but getting error;

update table1 as t1
set result = 
(select name from table2 where origin='UK') = any(string_to_array(t1.names, ','))
1
  • Can you update your post with the expected output table? Commented Jan 9, 2023 at 13:55

1 Answer 1

1

Use exists(...) if the result you want is boolean:

update table1 as t1
set result = exists(
    select name 
    from table2 
    where origin = 'UK'
    and name = any(string_to_array(t1.names, ','))
    );

Test it in db<>fiddle.

If you want to get the names, use string_agg():

update table1 as t1
set result = (
    select string_agg(name, ',') 
    from table2 
    where origin = 'UK'
    and name = any(string_to_array(t1.names, ','))
    );

Db<>fiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.