I have 27 tables in my database. One word table (a scrabble word list), and 26 association tables.
Table Fields
================
word [id,word]
a [word_id]
b [word_id]
...
z [word_id]
I’m trying to figure out matching words given a string.
For example, if the given string is pant, I want to know: pant, apt, pat, tap, ant, tan, nap, pan, at, ta, pa, an, na.
My current strategy is to explode each letter in the string and find the associated words that match all the letters.
For example:
SELECT word.word
FROM word, p, a, n, t
WHERE
word.id = p.word_id OR
word.id = a.word_id OR
word.id = n.word_id OR
word.id = t.word_id
But this ends up printing all words that have a p,a,n or t in them.
And if I switch all the operators to AND, I’m stuck with only one match: pant.
Can you help me solve this riddle?
I’m also concerned with how to handle duplicate letters in the string. For example, PPANT should find a match for app, when plain PANT should not.
Am I on the right track with the association tables or is there a better way?
I’m trying to handle this fairly efficiently in php/mysql. I’m aware there are others who have solved this riddle before in C, perl, java and the like.
I’m not familiar with MySQL’s advanced capabilities, so I cannot say if there is a way to do this restriction procedurally, which may save you a good amount of storage space. Nonetheless, I’ll offer this possibility.
Say this was your word table:
Then your query might look like the following:
Now, of course there are ways to normalize the table and multiple ways to create the query. You should experiment with what makes most sense for your situation.