【发布时间】:2015-10-29 15:43:48
【问题描述】:
我正在编写一个单词搜索程序。
我的数据库设置为 MyISAM 一张表(字)结构
WordID | String | A | B | ... | Z |
------------------------------------
int varchar int int ... int
其中 A - Z 列的值是该字母在字符串中出现的次数。
编写查询以查找由指定(但动态 - 用户选择)字符集(包括通配符)组成的所有可能单词,即:"Bu!!er" 应返回 but、butt、bull 等
在哪里
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 bu!!er,语句将是
`B` <= 1 AND `U` <= 1 AND `E` <= 1 AND `R` <= 1
第二部分
`A` + `C` + `D` + ... + `Z` <= 2
查询的完整Where部分变成了
( ( `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 )
还有比这更好的方法吗?
【问题讨论】:
-
是的。假设数据库甚至是正确的解决方案,请参阅规范化。数据库表不是电子表格。
-
还有什么其他的解决方案?现在正在研究正常化。谢谢。
-
好吧,搜索拼字游戏算法可能会提供一些其他想法。
-
IF(B-1 < 0, 0, B-1)-->GREATEST(B, 0) -
你的意思是
GREATEST (B-1, 0)?