数据库
首页 > 数据库> > MySQL,选择至少匹配X个字符的记录

MySQL,选择至少匹配X个字符的记录

作者:互联网

我正在尝试完成以下任务.假设我们有一个包含这些字段(ID,内容)的表

1 |苹果

2 |菠萝

3 |应用

4 |国家

现在,我正在寻找一个可以告诉我所有可能的常见匹配的函数.例如,如果参数为“ 3”,则该函数将返回出现在多于一个记录中的3个字符中的所有可能字符串.

在这种情况下,我得到“ app”,“ ppl”,“ ple”,“ ati”,“ tio”,“ ion”

如果参数为“ 4”,则得到:“ appl”,“ pple”,“ atio”,“ tion”

如果分位数为“ 5”,我得到:“苹果”,“ ation”

如果参数为“ 6”,则返回nohting.

直到现在,我还没有找到完成此任务的功能.

谢谢!

一些额外的信息:
我在带有MySQL数据库的PHP脚本中使用它.我真的只想提供字符数量作为参数,当然也要提供要搜索的表.

解决方法:

好吧,这有点丑陋,但确实可以正常工作.它是通用的SQL,可以在任何环境中使用.只需生成一个大于您正在读取的字段的最大长度的子字符串选择项即可.将函数中的数字50更改为超出字段长度的数字.它可能会返回一个很长的查询,但是就像我说的那样,它将很好地工作.这是Python中的示例:

import sqlite3

c = sqlite3.connect('test.db')

c.execute('create table myTable (id integer, content varchar[50])')
for id, content in ((1,'apple'),(2,'pineapple'),(3,'application'),(4,'nation')):
    c.execute('insert into myTable values (?,?)', [id,content])

c.commit();

def GenerateSQL(substrSize):
    subqueries = ["select substr(content,%i,%i) AS substr, count(*) AS myCount from myTable where length(substr(content,%i,%i))=%i group by substr(content,%i,%i) " % (i,substrSize,i,substrSize,substrSize,i,substrSize)  for i in range(50)]
    sql = 'select substr FROM \n\t(' + '\n\tunion all '.join(subqueries) + ') \nGROUP BY substr HAVING sum(myCount) > 1'
    return sql

print GenerateSQL(3)

print c.execute(GenerateSQL(3)).fetchall()

生成的查询如下所示:

select substr FROM 
    (select substr(content,0,3) AS substr, count(*) AS myCount from myTable where length(substr(content,0,3))=3 group by substr(content,0,3) 
    union all select substr(content,1,3) AS substr, count(*) AS myCount from myTable where length(substr(content,1,3))=3 group by substr(content,1,3) 
    union all select substr(content,2,3) AS substr, count(*) AS myCount from myTable where length(substr(content,2,3))=3 group by substr(content,2,3) 
    union all select substr(content,3,3) AS substr, count(*) AS myCount from myTable where length(substr(content,3,3))=3 group by substr(content,3,3) 
    union all select substr(content,4,3) AS substr, count(*) AS myCount from myTable where length(substr(content,4,3))=3 group by substr(content,4,3) 
    ... ) 
GROUP BY substr HAVING sum(myCount) > 1

其产生的结果是:

[(u'app',), (u'ati',), (u'ion',), (u'nat',), (u'pin',), (u'ple',), (u'ppl',), (u'tio',)]

标签:string-matching,mysql
来源: https://codeday.me/bug/20191024/1920643.html