2

I need to create a script searching for words with 'blanks', which basically are % in sql.

$numberofblanks = 1; //max 13
$searchedword = "WORD";
$searchedwordsorted = "DORW";

Results given should be:
WORDY WORLD CROWD SWORD WORDS DOWRY ROWED DROWN DOWER ROWDY

%word, w%ord, wo%rd, wor%d, word% would do, but what about more complicated queries, with 2 or more blanks?
Also was wondering if $searchedwordsorted is any helpful or it doesn't really matter and it's just waste of space in my table.

Thank you kindly for your help guys.
.mike

2
  • According to MySQL's Documentation % Matches any number of characters, even zero characters and not only blanks.
    – Havelock
    Aug 26, 2012 at 11:08
  • Well, you could use your sorted search word, sort all words in the DB, insert a wildcard after each character and then search for this pattern. Should find all possible matches (but it's probably slow)
    – knittl
    Aug 26, 2012 at 11:12

1 Answer 1

3

First I want to correct an error in your question. In your queries you mean _ not %. The % means any number of characters (zero or more). Use _ to mean exactly one character.

Now on to the solution... you don't actually need the sorted word stored in the database. You could just do this:

SELECT word
FROM dictionary
WHERE CHAR_LENGTH(word) = 6
AND word LIKE '%W%'
AND word LIKE '%O%'
AND word LIKE '%R%'
AND word LIKE '%D%'

If you have duplicate letters in your input, need to handle this correctly to ensure that all results contain all the duplicated letters. For example if the input is FOO__ you need to check that each word matches both %F% and %O%O%.

SELECT word
FROM dictionary
WHERE CHAR_LENGTH(word) = 5
AND word LIKE '%F%'
AND word LIKE '%O%O%'

Note that this approach will require a full scan of the table so it will not be particularly efficient. You could improve things slightly by storing the length of each word in a separate column and indexing that column.


If you have sortedword then you can improve performance by omitting the % between duplicated letters since you know that they will appear consecutively in sortedword. This could improve performance bceause it reduces the amount of backtracking required for failed matches.

SELECT word
FROM dictionary
WHERE CHAR_LENGTH(word) = 5
AND sortedword LIKE '%F%'
AND sortedword LIKE '%OO%'

Another approach that requires sortedword to be present is as follows:

SELECT word
FROM dictionary
WHERE CHAR_LENGTH(word) = 5
AND sortedword LIKE '%D%O%R%W%'

Again this requires a full scan of the table. Again, if you have repeated letters you don't need the % between them.

SELECT word
FROM dictionary
WHERE CHAR_LENGTH(word) = 5
AND sortedword LIKE '%F%OO%'
8
  • Ok, great. I'll definately improve my table adding 'wordlength'. Now, what about more complicated queries such as WOOORD****. ($numberofblanks= 4).
    – Michal
    Aug 26, 2012 at 11:19
  • @Michal: The only change you need to make when adding a blank is changing WHERE wordlength = 9 to WHERE wordlength = 10, for example. Nothing else needs changing in either query. That is, you want WHERE wordlength = $numberofblanks + $numberofnonblanks.
    – Mark Byers
    Aug 26, 2012 at 11:20
  • Fantastic. Thank you very much for your help. I'll get back to the topic, when I'm done and solving "SUBANAGRAMS" will be needed :) SUBANAGRAM = dow, row, dor, rod, ow, wo, do, od, or (all existing words containing letters from $sortedword).
    – Michal
    Aug 26, 2012 at 11:30
  • Ok, column wordlength added. I presume now istnead of WHERE CHAR_LENGTH(word) = 5 I could use WHERE wordlength = 5. Is that correct?
    – Michal
    Aug 26, 2012 at 11:51
  • @Michal: Yes. That change will give a performance improvement. Indexing (wordlength, sortedword, word) will probably also help.
    – Mark Byers
    Aug 26, 2012 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.