Redshift LIKE column value with %

徘徊边缘 提交于 2020-01-05 07:54:05

问题


I have a column that is a comma separated string of values. I want to join another table that only has one of the values. On redshift, how can I do a LIKE operator with '%' injected into the comparison?

Ex:

TableA: values_col = 'abc, def'

TableB: value_col = 'def'

SELECT *
FROM TableA a
JOIN TableB b ON b.value_col LIKE '%' || a.values_col || '%'

The above concat doesn't seem to work. Any suggestions would be appreciated. Thanks.


回答1:


You will get awful performance. You should fix your data structure. But if you have to, then this should work:

SELECT *
FROM TableA a JOIN
     TableB b
     ON ',' || a.values_col || ',' LIKE '%,' || b.value_col || ',%';

The commas are important if your values can contain each other. More importantly, the like needs the operands in the right order.



来源:https://stackoverflow.com/questions/48777225/redshift-like-column-value-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!