4

I have 2 tables, say table A has 10 rows and table B has 100 rows and I want to perform a join, but the matching condition has to be where a column from A 'is like' a column from B meaning that anything can come before or after the column in B:

For example: if the column in A is 'foo'. Then the join would match if column in B is either: 'fooblah', 'somethingfooblah', or just 'foo'. I know how to use the wildcards in a standard like statement, but am confused when doing a join. Does this make sense? Thanks.

This Code doesn't work:

SELECT *
FROM TABLE a JOIN
     TABLE b
     ON b.column LIKE CONCAT('%', a.column, '%');

Example:

Table A
+-------+
| MYCOL | 
+-------+
| foo   |
| foo   |
| bar   |
| bbb   |
| bar   |
+-------+

Table B
+------------------+
| MYCOL            |
+------------------+
| fooblah          |
| somethingfooblah |
| foo              |
| barblah          |
| somethingbarblah |
| bar              |
+------------------+

Result:
+-------+------------------+
| MYCOL | MYCOL            |
+-------+------------------+
| foo   | fooblah          |
| foo   | somethingfooblah |
| foo   | foo              |
| --    | test             |
| bar   | somethingbarblah |
| bar   | bar              |
+-------+------------------+
SandPiper
  • 2,816
  • 5
  • 30
  • 52
M Dogan
  • 41
  • 1
  • 3

2 Answers2

4

From the HANA documenation for CONCAT it appears that CONCAT() only takes two parameters, not three or more. You can workaround this by just nesting two calls to CONCAT():

SELECT *
FROM TABLE_a JOIN TABLE_b
    ON b.column LIKE CONCAT('%', CONCAT(a.column, '%'));
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0
   SELECT a.employee_id
       FROM employees a
         JOIN departments b
           ON a.department_id LIKE '%'
             ||a.department_id
             || '%';
Kaushik Nayak
  • 30,772
  • 5
  • 32
  • 45