您可以将exists 与instr 一起使用:
select t1.full_name, exists (select 1 from table_2 t2 where instr(t1.full_name, t2.name) > 0) status from table_1 t1;
输出:
| full_name |
status |
| Crystal, Crystal |
1 |
| Carmen, TEST2 |
1 |
| XYZ, ABC |
0 |
| BLA, VVV |
1 |
编辑:上面的解决方案将匹配任何出现的子字符串,但是,如果您希望匹配仅基于逗号分隔的值,您可以使用递归 cte 来获取子字符串:
with recursive vals(id, n) as (select row_number() over (order by (select 1)), t.* from table_1 t),
cte(id, v, r) as (
select id, case when instr(n, ", ") > 0 then substr(n, 1, instr(n, ", ")-1) else n end, case when instr(n, ", ") > 0 then substr(n, instr(n, ", ")+2, length(n) - instr(n, ", ")) else "" end from vals
union all
select id, case when instr(r, ", ") > 0 then substr(r, 1, instr(r, ", ")-1) else r end, case when instr(r, ", ") > 0 then substr(r, instr(r, ", ")+2, length(r) - instr(r, ", ")-1) else "" end from cte where length(r) > 0
)
select s1.n, s3.c from (select s.id, max(s.v in (select * from table_2)) c from cte s group by s.id) s3 join vals s1 on s3.id = s1.id;
见demo。