음,보기 흉한 일이지만 잘 작동합니다. 이것은 일반적인 SQL이며 모든 환경에서 작동합니다. 단순히 읽고있는 필드의 최대 길이보다 큰 부분 문자열을 선택하십시오. 함수의 숫자 50을 필드 길이를 초과하는 숫자로 변경하십시오. 그것은 realllly 긴 쿼리를 반환 할 수 있지만 내가 말했듯이, 그것은 잘 작동합니다.
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
작성된 결과는 다음과 같습니다 :
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()
쿼리의 모양을 생성 : 여기 파이썬에서 예입니다
[(u'app',), (u'ati',), (u'ion',), (u'nat',), (u'pin',), (u'ple',), (u'ppl',), (u'tio',)]
길이를 나타내는 숫자 외에도 쿼리에 대한 인수로 무엇을 보내시겠습니까? 이것이 어떤 언어를 기반으로 할 것입니까? –