问题
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 |
+-------+------------------+
回答1:
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, '%'));
回答2:
SELECT a.employee_id
FROM employees a
JOIN departments b
ON a.department_id LIKE '%'
||a.department_id
|| '%';
来源:https://stackoverflow.com/questions/44485210/how-to-join-if-the-text-contains