将有关歌曲的信息导入我的SQLite数据库后,我想使用SELECT语句使用以下条件查找所有可能的重复歌曲:
一行中的songName与同一张表(“歌曲”)中任何其他行中的songName相似或相等,并且artistID在这两行中相同。这应该在不知道songName内容的情况下起作用。如果我想将一个已知的歌曲名称与数据库中的所有其他歌曲名称进行比较,可以使用“ 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
编辑:
由于提出了创建哈希并比较它们的想法,因此我正在考虑使用此psuedo函数为每个歌曲名称创建哈希:
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代码查看返回的记录的artistID是否相同。
到目前为止,该解决方案似乎运行良好。这是我用来查找重复歌曲的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
最佳答案
我个人会添加一个额外的字段,在其中您可以计算标题的某种“哈希”。一个好的功能是剥离所有非字母字符(包括空格),删除任何文章(例如“ the”,“ a”,“ an”),然后计算标题的soundex code并在其前面加上artistId串。
因此,在您的情况下,您将获得:
id songName artistID duration Hash
----------------------------------------------------
0 This is a song 5 3:43 5.T0021
1 Another song 3 3:23 3.A9872
2 01-This is a song 5 3:42 5.T0021
3 song 4 4:01 4.S0332
4 song 4 6:33 4.S0332
5 Another record 2 2:45 2.A7622
从现在开始,仅获取具有... count(Hash)> 1的行应该很容易...
还要注意,我建议使用Soundex,但是您可以使用自己的功能,也可以改编现有的功能,使某些元素比其他元素更相关。
关于sql - 使用LIKE选择所有可能重复的相似行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5589701/