0

My problem is this; I am having problems using sql to match parts of a query. For example I want a statement like this:

 SELECT * FROM {table} WHERE {table}.title LIKE '%term%';

Where term is some search term and table is the table I am querying. This statement works fine in almost all cases. The problem occurs when the query term is longer than what is in the database. For example:

  (assuming there is an entry in {table} with title="foobar")
  SELECT * FROM {table} WHERE {table}.title LIKE '%foobars%';

The above will not match foobars = foobar because of the s.

What can I use to overcome this?

Thank you.

user2980357
  • 11
  • 1
  • 2
  • try `LIKE '%foobar%'` – Braj Jul 21 '14 at 15:43
  • 2
    You can't do anything, not with this simple wildcard search. Your "find this" string is longer than what exists in your DB, therefore that string really doesn't exist in the DB. – Marc B Jul 21 '14 at 15:46
  • 1
    You might want to look into the `SOUNDEX` function: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex – Dave Zych Jul 21 '14 at 15:50

1 Answers1

0

In sql you can do 'somestring' LIKE CONCAT('%',title,'%') which is a reverse like statement but you have to be careful, since the performance is quite slow. But for your problem you should try:

(assuming there is an entry in {table} with title="foobar")
SELECT * FROM {table} WHERE '%foobars%' LIKE CONCAT('%', {table}.title,'%');
gaw
  • 1,960
  • 2
  • 14
  • 18