views:

115

answers:

4

I need to write what I'd like to call a Valley Girl-query. I need to SELECT something that's LIKE IN - something like this:

SELECT * FROM Table1 WHERE Name LIKE IN ( SELECT Name FROM Table2 )

The reason for this is I've got a table full of company names, but they're not exactly the same - f.ex. in Table1 it might say "Chrysler Group", while in Table2 it might just say "Chrysler".

Is there any easy way to do this?

A: 

I think:

SELECT * FROM table1 WHERE EXISTS (SELECT Name FROM Table2 WHERE Table1.Name LIKE Name)
Murph
You should view the execution plan for your suggestion.
silent
? If you're going to make that sort of a comment you ought to explain *why* - I'm quite willing to learn (its why the answer is prefixed "I think:") and quite willing to delete the answer if its wrong or edit/annotate as appropriate but a bit more help in that direction first is desirable
Murph
A bit old, but I'm bored: The problem with your query is that the WHERE EXISTS will be called once per row in Table1. As the WHERE EXISTS also uses a LIKE operator, performance will likely be very poor. Given the original request, I think any query would perform slowly, so don't take this to heart :)
Meff
Thanks @Meff - that last is a fairly key point.
Murph
+2  A: 
select
  *
from
  Table1 t1
    inner join Table2 t2 on (t1.name like t2.name + '%')

or without '%' sign if you want :)

silent
That's one ssslllooowww query! BUT it did answer my question. Unfortunately the resulting data wasn't good enough for what I needed it for, but that's not really my problem. ;)
Marcus L
You should use a company table and use its identifiers in your other tables. It will be more comfortable to work with it and a great speedup.
silent
Table2 is actually a temporary table for this one-time export run I was going to do, thus the not so best practice approach.
Marcus L
+1  A: 

Hi,

Here's one way you could do it:

SELECT t1.*
FROM Table1 t1
    JOIN Table2 t2 ON t1.Name LIKE t2.Name + '%'
AdaTheDev
A: 

If you are trying to create a list of close matches then the SOUNDEX function can be helpful when there may be questionable spelling.

SELECT T1.* 
  FROM Table1 T1    
  JOIN Table2 T2 ON SOUNDEX(T1.Name) = SOUNDEX(T2.Name)
Andrew