0

I've got a table full of substrings, and I need to select all substrings, which are in the search string.

Basically it would be the mirrored form of a where-condition:

WHERE "SearchString" LIKE "%"+ currentColumnValue +"%"

I know, that this is not possible, but for performance reasons I don't want to iterate every single database entry.

Maybe you have got an idea how to solve this kind of problem?

1
  • 1
    Why do you think it's not possible? Commented Dec 10, 2014 at 16:45

2 Answers 2

3

You can do

where 'SearchString' like concat('%', columnValue, '%');

This will be very slow as it would do a table scan and a like-compare on each line, but the result is correct.

http://sqlfiddle.com/#!2/e2b066/1

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, that's it! What do you think? Is it better to perform this task on MySQL-side or on PHP-side?
It's better on the MySQL side. If you're doing it in PHP you basically have to read the entire table into PHP and filter each row. The added cost is the network traffic that happens before the filter. In MySQL you still have to filter on each row but there is no network for rows not matching.
0

You can try this

$value = //your value

' select * from tb_name where col_name like %".$value."% '

1 Comment

Sorry, you did not understand my question. That's the normal way of searching. I'm not searching for a row which contains my search string, but for a row which value is a substring of my (much longer) search string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.