Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL join query using like?

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 !

like image 813
David Avatar asked Dec 18 '09 21:12

David


People also ask

Can you join using like?

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".

Which two operators can be used in an outer join condition a B or C in D and?

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.

Which of the following joints is are available in MySQL?

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.


2 Answers

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.

like image 170
Tor Valamo Avatar answered Sep 22 '22 22:09

Tor Valamo


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.

like image 45
brettkelly Avatar answered Sep 20 '22 22:09

brettkelly