Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining tables with LIKE (SQL)

First of all I am using Oracle:

Table One Name = tableone

Table Two Name = tabletwo

tableone has a column named pizzaone, tabletwo has a column named pizzatwo. I want to join tableone to tabletwo where pizzaone is somewhere in the pizzatwo's name.

What I tried:

select * 
from tableone 
   join tabletwo on tableone.pizzaone like ('%' + tabletwo.pizzatwo + '%')

How can I correct this query?

like image 625
Jacob Nelson Avatar asked Mar 07 '11 20:03

Jacob Nelson


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

What are joins like in SQL?

Different Types of SQL JOINs (INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.

How do you join two similar tables?

To join two tables based on a column match without loosing any of the data from the left table, you would use a LEFT OUTER JOIN. Left outer joins are used when you want to get all the values from one table but only the records that match the left table from the right table.

Can you combine in and like in SQL?

Is there as way to combine the "in" and "like" operators in Oracle SQL? Answer: There is no direct was to combine a like with an IN statement. However Oracle does support several alternative clauses: CONTAINS clause: the contains clause within context indexes.


1 Answers

Try this syntax instead:

select * 
from tableone 
   join tabletwo on tableone.pizzaone like ('%' || tabletwo.pizzatwo || '%')

Oracle's string concatenation operator is the double pipe (||). The invalid number error is because Oracle expects numeric operands for the '+' operator.

like image 97
DCookie Avatar answered Sep 22 '22 17:09

DCookie