I have been trying to get this working for quite a while now but it just doesn't seem to work, maybe it is is not even possible, what i am wanting to do is to perform a MySQL join query using like, such as this example i found...
SELECT * FROM Table1 INNER JOIN Table2 ON Table1.col LIKE '%' + Table2.col + '%'
but it just doesn't seem to work at all, any help that can be given would be brilliant, thanks !
Is this possible using LIKE or LEFT ? Depends on what type of sql server you're using for the syntax you need. The simple answer is "yes, this is possible".
To write a query that performs an outer join of tables A and B and returns all rows from B (a right outer join), use the RIGHT [ OUTER ] JOIN syntax in the FROM clause, or apply the outer join operator (+) to all columns of A in the join condition in the WHERE clause.
MySQL supports the following types of JOIN clauses: INNER JOIN, OUTER JOIN, and CROSS JOIN. OUTER JOINs can further be divided into LEFT JOINs and RIGHT JOINs. To better demonstrate how the JOINs work, we will create two tables.
Try
SELECT * FROM Table1 INNER JOIN Table2 ON Table1.col LIKE CONCAT('%', Table2.col, '%')
MySQL does string concatenation differently from other databases, so in case you want to port your app, you need to have an alternate version where ||
is used as concatenation operator, as mentioned by Michael in another answer. This operator doesn't work in MySQL though, since it means or
.
How about this instead:
SELECT * FROM Table1, Table2 WHERE Table1.col LIKE '%'+Table2.col+'%';
Since you're selecting everything from both tables anyway, this would probably get you there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With