【发布时间】:2011-04-08 02:14:29
【问题描述】:
将有关歌曲的信息导入我的 SQLite 数据库后,我想使用 SELECT 语句来查找所有可能使用此条件的重复歌曲:
一行中的歌曲名称与同一表(歌曲)中任何其他行中的歌曲名称相似或相等,并且两行中的艺术家 ID 相同。这应该在不知道歌曲名称的内容的情况下工作。如果我想将已知歌曲名称与数据库中的所有其他歌曲名称进行比较,可以使用“songName LIKE '%known name%'”来完成,但是如果没有这个,我如何找到所有重复项?
示例歌曲表:
id songName artistID duration
--------------------------------------------
0 This is a song 5 3:43
1 Another song 3 3:23
2 01-This is a song 5 3:42
3 song 4 4:01
4 song 4 6:33
5 Another record 2 2:45
预期结果:
id songName artistID duration
--------------------------------------------
0 This is a song 5 3:43
2 01-This is a song 5 3:42
3 song 4 4:01
4 song 4 6:33
编辑:
由于提出了创建哈希并比较它们的想法,我正在考虑使用这个伪函数为每个歌曲名称创建一个哈希:
Public Function createHash(ByVal phrase As String) As String
'convert to lower case
phrase = LCase(phrase)
'split the phrase into words
Dim words() As String = phrase.Replace("_", " ").Split(" ")
Dim hash As String = ""
For w = 0 To words.Count - 1
'remove noise words (a, an, the, etc.)
words(w) = removeNoiseWords(words(w))
'convert 1 or 2-digit numbers to corresponding words
words(w) = number2word(words(w))
Next
'rebuild using replaced words and remove spaces
hash = String.Join("", words)
'convert upper ascii into alphabetic (ie. ñ = n, Ö = O, etc.)
hash = removeUnsupChars(hash, True)
'strip away all remaining non-alphanumeric characters
hash = REGEX_Replace(hash, "[^A-Za-z0-9]", "")
Return hash
End Function
计算完哈希后,我会将其与每条记录一起存储,然后使用 count(hash)>1 选择重复项。然后,我将使用 .NET 代码查看返回的记录的艺术家 ID 是否相同。
到目前为止,此解决方案似乎运行良好。这是我用来查找重复歌曲的 SQLite 语句:
SELECT count(*),hash from Songs GROUP BY hash HAVING count(hash) > 1 ORDER BY hash;
这给了我一个多次出现的所有哈希的列表。我将这些结果存储在一个数组中,然后循环遍历该数组并简单地使用此语句来获取详细信息:
For i = 0 To dupeHashes.Count - 1
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "SELECT * from Songs WHERE hash = '" & dupeHashes(i) & "';"
SQLreader = SQLcommand.ExecuteReader()
While SQLreader.Read()
'get whatever data needed for each duplicate song
End While
SQLcommand.Dispose()
SQLconnect.Close()
Next
【问题讨论】:
-
SQLite Full Text Search 用于检索匹配的行。
-
都在markdown documentation - 添加/编辑问题时可以访问的链接:(
-
我明白了。回到你的建议。我快速浏览了全文搜索,但你能提供一个例子吗?我不知道如何比较两个未知数(歌曲名称“x”与所有其他歌曲名称)。
-
“简单 FTS 查询”提供了如何使用 FTS 进行搜索的示例
-
是我一个人,还是我们最近看到很多歌曲相关的sql问题。都有类似的sql表结构,但问的不是同一个问题...奇数