views:

341

answers:

3

How can I do a SQL query with the like operator from two different tables?

I need something like:

select * from table1 where name like %table2.name

It's not a common field but a substring of a field on another table.

+3  A: 

Not particularly sure about the precise syntax, but here's an idea:

select ... from (
    select ID, Foo as F from FooTable
    union all
    select ID, Bar as F from BarTable) R
where R.F like '%text%'
Anton Gogolev
Your alias is `F`, so it should be `R.F like`, not `R.Foo like`.
Peter Lang
@Peter: Thanks! Corrected that.
Anton Gogolev
it's not what i mean!! i nedd something like: select * from table1 where name like %table2.name
simone
Would you be much better off putting the filter on the individual selects? You're creating a potentially huge result set as a union, and only then filtering. If you filter first, the statement will be less complex (eliminating a SELECT) and I'd think it would execute faster: `SELECT ID, Foo as F FROM FooTable WHERE Foo LIKE '%text%' UNION ALL SELECT ID, Bar as F FROM BarTable WHERE Bar LIKE '%text'`
T.J. Crowder
no it's just filterin the content that is a substring of the one in another table
simone
@simone: If so, this is not my fault. You should have been more precise in your original question. GIGO in its full glory :)
Anton Gogolev
+3  A: 

Edit

(original answer is further down)

Your comment (and subsequent edit) completely changes the question.

To do that, you can use LIKE as part of the ON clause in a join:

CREATE TABLE a (foo varchar(254))
GO

CREATE TABLE b (id int, bar varchar(254))
GO

INSERT INTO a (foo) VALUES ('one')
INSERT INTO a (foo) VALUES ('tone')
INSERT INTO a (foo) VALUES ('phone')
INSERT INTO a (foo) VALUES ('two')
INSERT INTO a (foo) VALUES ('three')

INSERT INTO b (id, bar) VALUES (2, 'ne')
INSERT INTO b (id, bar) VALUES (3, 't')

SELECT a.foo
FROM a
INNER JOIN b ON a.foo LIKE '%' + b.bar
WHERE b.id = 2

(That's the SQL Server version; for MySQL, add in the various semicolons, remove the GOs, and use ...LIKE concat('%', b.bar) instead.)

That uses id = 2 to find bar = "ne" in table b, then prepends the % operator and uses it to filter results from a. Results are:

one
tone
phone

You won't have to do the concat if you can store the operator in b.bar.

Separately, I was surprised to find that this works (on SQL Server) as well:

SELECT foo
FROM a
WHERE foo LIKE (
    SELECT TOP 1 '%' + bar
    FROM b
    WHERE id = 2
)

...but the version using JOIN is probably more flexible.

That should get you going.

Original answer

(Arguably no longer relevant)

It's hard to tell what you're asking, but here's an example of using LIKE to limit the results from a JOIN:

SELECT a.foo, b.bar
FROM someTable a
INNER JOIN someOtherTable b
    ON a.someField = b.someField
WHERE a.foo LIKE 'SOMETHING%'
AND b.bar LIKE '%SOMETHING ELSE'

That will give you foo from someTable and bar from someOtherTable where the rows are related by someField and foo starts with "SOMETHING" and bar ends with "SOMETHING ELSE".

T.J. Crowder
i need something like: select * from table1 where name like %table2.name
simone
A: 

I want to select multiple users from two tables. Both employees are both tables. i want to display them and than delete them from both tables at the sametime. Below is what i have and i am getting error " ORA-01797: this operator must be followed by ANY or ALL "

select * from table A, table B where a.OPRID =('employee1','employee2') AND A.OPRID = B.OPRID

Syed