0

I have two tables, lets call it table A and table B. They looks like:

A.col1        A.colX            B.colY
1             123               123
2             234, 123          234     
3             2, 52352          2
                                52352

What I have to do is to JOIN them, most probably using WHERE, because non-equi join is not supported in my environment. I have the problem with table A, where numbers are as string. I tried solution with:

SELECT... 
FROM A, B
WHERE A.colX LIKE concat('%', B.colY, '%')

but it doesn't work correctly as for example value 2 from B.colY appears in every string from table A.colX.

At the end example what i want to achieve:

          output
1         123 -------> 123
2         234, 123 --> 234
1         234, 123 --> 123
3         2, 52352 --> 2
3         2, 52352 --> 52352

Do you have any ideas for that ?

4

2 に答える 2

2
where (',' || replace(A.colX,' ','') || ',') LIKE ('%,' || B.colY || ',%')

つまり、A.colX からスペースを削除し、前後にコンマを追加してから、B.colY にもコンマを追加likeします。

||は ANSI SQL 方式の連結であり、一部の製品では代わりにCONCATorを使用します。+

于 2015-10-02T12:11:55.380 に答える
0

文字列を配列に split() してから unnest() して各部分に個別の行を生成し、通常の結合を実行できます。

https://prestodb.io/docs/current/functions/string.html#split

https://prestodb.io/docs/current/sql/select.html#unnest

于 2015-10-02T14:59:26.543 に答える