Jump to content

Question about a query I thought would be a lot simpler!


jbradley04

Recommended Posts

Hello,

I am trying to run MySQL query that will take a bunch of letters and do a couple things.

First:

If they enter "drea" then I want to search for all words in my DB that contain those letters and only those letters.  So the result will be"

read

dear

ear

red

etc.....

 

Next I want to run another query that would have Exactly the letters in the word.

So the same query "drea" would only come up with:

read

dear

 

Any help would be greatly appreciated!

Thanks!

Link to comment
Share on other sites

Quick question.  When I use the OR command it returns words that contain more than what I want.

For example:

If I search "ogf"

Right now it would come up with friend, hog, ogor, etc... because they contain at least one of those letters.  

I am looking for it to come back with ONLY words that contain the letters searched. 

So ogf would only come up with:

fog

of

 

But no other words that only contain one or all those letters but also have other letters too.  

 

Hope that makes sense!

Thanks

Link to comment
Share on other sites

i'm not sure you can do all of this in a query, but one fairly efficient way (over forming all the permutations of the target letters) would be -

 

1) use a query to fetch all words that have any of the target letters. you should be able to use a mysql REGEXP match to do this without building the list of (... like) OR (... like )... terms.

 

2) when retrieving the words from the query, remove all the target letters from the words and any result that is zero length was made up of only the target letters. any result that has a non-zero length after removing the target letters isn't a possible word.

Link to comment
Share on other sites

it turns out a REGEX in the query directly solves this -

SELECT word FROM words WHERE word REGEXP '^[drea]+$'

and if you throw in a length check, it will do your second assignment -

SELECT word FROM words WHERE word REGEXP '^[drea]+$' AND CHAR_LENGTH(word) = 4
Edited by mac_gyver
Link to comment
Share on other sites

Any thoughts?

After you query for possible matches, do a little post processing to ensure that letters are not used more than they are allowed. Create a function that given a word and list of letters will return true/false depending on if the word can be created with those letters. Then, as you loop the results, check each word:

function IsValidWord($word, $letters){
  //...Implement this function
  //...array_count_values and str_split may be helpful
}

$validWords=array();
while ($row=$query->fetch()){
   if (IsValidWord($row['word'], $searchLetters)){
      $validWords[] = $row;
   }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.