【问题标题】:Query where sum of specified columns (all ints) in row are less than a specified int查询行中指定列(所有整数)的总和小于指定整数的位置
【发布时间】: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 &lt; 0, 0, B-1) --> GREATEST(B, 0)
  • 你的意思是GREATEST (B-1, 0)

标签: php mysql sql myisam


【解决方案1】:
 `A` + `C` + `D` + ... + `Z`

使用非规范化?将完整长度存储在单独的列中。

 `TOTAL` <= 5

作为旁注:

您的架构过多地限制了可能的查询 - 尽管对于这项工作来说已经足够了。最好将所有单词保留在内存中(每个服务器实例一个)并对单词进行“全表扫描”或“索引扫描”。

【讨论】:

  • 存储全长的问题在于 - A + C + D + ... + Z - 这些是用户未指定的字符,所以它们每次都会根据用户的输入而变化。为什么说它限制了可能的查询?感谢您的宝贵时间
  • 它不能解决这些类型的查询,但我认为它可以解决您的 original 问题:“编写查询以查找由指定集合组成的所有可能单词字符数(包括通配符)”。所以我建议的是查询的第二部分。
  • 明白了。在那种情况下,我会看到它是如何工作的。我应该更清楚。
猜你喜欢
  • 2010-12-02
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多