[英]mysql select all records where a datetime field is less than a specified value
[英]Query where sum of specified columns (all ints) in row are less than a specified int
我正在寫一個單詞搜索程序。
我的數據庫設置為MyISAM,其中一個表(單詞)結構化
WordID | String | A | B | ... | Z |
------------------------------------
int varchar int int ... int
其中A-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
查詢的完整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 )
有沒有比這更好的方法了?
`A` + `C` + `D` + ... + `Z`
使用非規范化? 將全長存儲在單獨的列中。
`TOTAL` <= 5
作為旁注:
您的架構過多地限制了可能的查詢,盡管足以完成此工作。 最好將所有單詞保留在內存中(每個服務器實例一個),並對單詞進行“全表掃描”或“索引掃描”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.