PostgreSQL:如何检查值是否在文本中的值列表中

PostgreSQL: How to check if the value is in the list of values within the text

我在一列中有一个值,在另一列中有一个值字符串,那么如何检查值列表中是否有一个值? 目标是找到一个确切的值,即只有 'L',而不是 'L' 作为 'XL'

的一部分
with cte as
(
select 'L' as test_char, 'S, M, L, XL, XXL, 3XL' as test_column
union
select 'L', '30, 40, 50, 60, 70'
union
select 'L', '30L, 40X, 50M, 60XL, 70XXL'
)   
select test_char, 
       test_column,
       case when test_char in (test_column) then 'yes' else 'no' end as check_column
from cte

我尝试用 in 查询,但找不到。 (在第三行中 check_column 需要是 'yes')。

是否可以将字符串中的值转换为数组并检查数组中的每个值?或者有什么其他想法?

您似乎想像 array 一样对待您的字符串并查看每个成员。看起来你的 test_column 是由 `", " 分隔的,所以你可以使用下面的

, string_to_array(test_column,', ') @> array['L']

来自文档

Operator    Description Example                     Result
@>          contains    ARRAY[1,4,3] @> ARRAY[3,1]  t

Docs