Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有更好的方法使用SQL查找字谜?_Sql - Fatal编程技术网

有没有更好的方法使用SQL查找字谜?

有没有更好的方法使用SQL查找字谜?,sql,Sql,给定以下数据库表: WORDS alphagram....varchar(15) word.........varchar(15) PK length.......int 其中: “字母表”是按字母顺序排列的单词字母(例如,AEINNRTT是内部网的字母表) 主键是“word”,有字母和长度索引 我找到了一种通过SQL查找给定字母串的字谜的方法。例如,要查找AEINNRTT的字谜,这将起作用: select alphagram, word, definition from words

给定以下数据库表:

WORDS
 alphagram....varchar(15)
 word.........varchar(15) PK
 length.......int
其中:

  • “字母表”是按字母顺序排列的单词字母(例如,AEINNRTT是内部网的字母表)
  • 主键是“word”,有字母和长度索引
我找到了一种通过SQL查找给定字母串的字谜的方法。例如,要查找AEINNRTT的字谜,这将起作用:

select alphagram, word, definition
from words
where length = 8
and alphagram like '%A%' 
and alphagram like '%E%' 
and alphagram like '%I%'
and alphagram like '%NN%' 
and alphagram like '%R%' 
and alphagram like '%TT%'
将返回1行(对于INTRANET)

如果我想包含已知数量的通配符,例如,内联网+空白(通配符)的单词数,我只需将“长度”更改为字母总数+通配符数

e、 g

…将返回8行(娱乐、即时、集成、内部网、巡回、闲聊、吵闹和暂时)

我的问题是:是否有一种更有效的方法仅通过SQL来实现这一点


这在SQLServer中运行非常快,但在SqlLite中运行非常慢。我意识到%xxx%的搜索速度并不快。

一个想法是这样做(对于给定的字长):

  • 将单词拆分为单个字符(可能在循环中使用
    SUBSTRING()
    ,但更好的方法可能值得单独提问)

  • 利润


不过,正如一位评论员所说,我强烈建议您在SQL之外这样做,除非您有很好的理由不这样做,或者您这样做只是为了挑战您的技能。

您可以为每个条目创建一种索引列,其中包含单词的所有字母,并按字母顺序进行比较。每个字谜都有相同的索引值。

我想最好的方法是: 我创建了a…z列 然后分析每个单词,计算给定字母出现的次数,并将其放在相应的列下 接下来,当我输入要解读的单词时,我计算了该单词每个字母出现的次数,并将其与数据库中的单词进行了比较
这可能有点难以理解。如果您需要进一步澄清,请告诉我。

这个问题很老,我可能会误解,但您的第一个请求可能是

select alphagram, word, definition
from words
where length = 8
and alphagram = 'AEINNRTT' and word <> alphagram

您使用SQL而不是应用层有什么原因吗?我试图让事情简单一些,但我可能不得不这样做。
select alphagram, word, definition
from words
where length = 8
and alphagram = 'AEINNRTT' and word <> alphagram
select alphagram, word, definition
from words
where length = 9
and alpha_a >= 1
and alpha_e >= 1
and alpha_i >= 1
and alpha_n >= 2
and alpha_r >= 1
and alpha_t >= 2