我正在編寫一個單詞搜索程序。查詢行中指定列(所有整數)的總和小於指定的整數
我的數據庫是建立在MyISAM 一個表(字)結構化
WordID | String | A | B | ... | Z |
------------------------------------
int varchar int int ... int
其中用於列的值 - Z的字符串中該字母出現的#。
要編寫一個查詢來尋找出一個指定的(但動態 - 用戶選擇)的所有可能的字的字符集(包括野生字符),即:"Bu!!er"
應返回,但屁股,牛等
在哪裏
S is the set of characters specified that we can use
W is the set of characters in a word
我需要查詢數據庫的所有字符串,其中
# of occurences in the word for each specified character (not including "!") is less than number of occurrences of that character in the specified string
W_k < S_k where k is each character specified
和
# of occurrences of letters not specified in the specified string are in SUM less than the total occurrences of the wildcard character ("!") in the specified string
W_q < S_! where q is each character not specified and S_! total amount of occurrences of "!".
爲WHERE
聲明的第一部分(W_k < S_K) 對於bu!!er
的聲明將是
`B` <= 1 AND `U` <= 1 AND `E` <= 1 AND `R` <= 1
而對於第二部分
`A` + `C` + `D` + ... + `Z` <= 2
完全在哪裏部分查詢變爲
((`A` + (IF(`B`-1 < 0, 0, `B`-1)) + `C` + `D` + (IF(`E`-1 < 0, 0, `E`-1)) + `F` + `G` + `H` + `I` + `J` + `K` + `L` + `M` + `N` + `O` + `P` + `Q` + (IF(`R`-1 < 0, 0, `R`-1)) + `S` + `T` + (IF(`U`-1 < 0, 0, `U`-1)) + `V` + `W` + `X` + `Y` + `Z`) <= 2)
有沒有比這更好的方法呢?
是的。假設數據庫甚至是適當的解決方案,請參閱規範化。數據庫表格不是電子表格。 – Strawberry
還有什麼其他解決方案?現在正在研究正常化。謝謝。 –
好吧,搜索拼字遊戲算法可能會提供一些其他的想法。 – Strawberry