Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner join with like clause

Tags:

sql

mysql

I am using inner join with the like clause ..

My tried sql is

SELECT tbl_songs.id    AS sid, 
       tbl_songs.name  AS sname, 
       tbl_albums.id   AS aid, 
       tbl_albums.name AS aname 
FROM   tbl_songs 
       INNER JOIN tbl_albums 
               ON tbl_songs.albums LIKE '%' + tbl_albums.name + '%'; 

Its showing me syntax error.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '+ tbl_albums.name + '%'' at line 2

Please elaborate reason of syntax error.

like image 310
Sankalp Avatar asked Jun 08 '13 19:06

Sankalp


People also ask

Can we use like operator in join condition?

The Like operator is used to search the pattern in one table. Similarly, we can use the like operator along with the concatenation function to do a pattern matching between the multiple tables.

Can you join using like?

The simple answer is "yes, this is possible".

Which join is like inner join?

SQL Inner Join clause is the same as Join clause and works the same way if we don't specify the type (INNER) while using the Join clause. In short, Inner Join is the default keyword for Join and both can be used interchangeably.

Can WHERE clause be used with inner join?

To use the WHERE clause to perform the same join as you perform using the INNER JOIN syntax, enter both the join condition and the additional selection condition in the WHERE clause. The tables to be joined are listed in the FROM clause, separated by commas.


2 Answers

you have to form the clause using concat ...

...LIKE CONCAT('%',tbl_albums.name, '%');

there is no + operator like this in mysql

like image 155
The Surrican Avatar answered Oct 18 '22 09:10

The Surrican


You can use below format in oracle sql:
  SELECT tbl_songs.id    AS sid, 
           tbl_songs.name  AS sname, 
           tbl_albums.id   AS aid, 
           tbl_albums.name AS aname 
    FROM   tbl_songs 
           INNER JOIN tbl_albums 
                   ON tbl_songs.albums LIKE ('%'||tbl_albums.name||'%'); 
like image 36
rcmuthu786 Avatar answered Oct 18 '22 07:10

rcmuthu786