Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to join, if the text contains

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              |
+-------+------------------+
like image 406
M Dogan Avatar asked Sep 02 '25 14:09

M Dogan


1 Answers

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, '%'));
like image 178
Tim Biegeleisen Avatar answered Sep 05 '25 04:09

Tim Biegeleisen