7

我正在尝试在 SQL Server 中编写一个查询,以确定每个哈希值是否有多行。
我需要哈希值重复的所有文件名。

结果应该是(基于我下面的例子)

003B4C68BC143B0290E04432A3A96092    File0003.jpg
003B4C68BC143B0290E04432A3A96092    File0004.jpg
003B4C68BC143B0290E04432A3A96092    File0005.jpg

请告诉我。

这是表结构

File table
-----------------------------------------
hash          FileName
---------------------------------------
000341A486F5492877D588BED0806650    File0001.jpg
00363EF2ECEEA32F10176EB64A50283F    File0002.jpg
003B4C68BC143B0290E04432A3A96092    File0003.jpg
003B4C68BC143B0290E04432A3A96092    File0004.jpg
003B4C68BC143B0290E04432A3A96092    File0005.jpg
4

2 回答 2

15
select * 
from File 
where hash in (select 
               hash 
               from File
               group by hash
               having count(*) > 1)
于 2013-05-22T09:12:54.627 回答
4

您可以EXISTS用来检查重复项,

SELECT  a.*
FROM    TableName a
WHERE   EXISTS
        (
            SELECT  1
            FROM    Tablename b
            WHERE   a.hash = b.hash
            GROUP   BY hash
            HAVING  COUNT(*) > 1
        )

或者INNER JOIN

SELECT  a.*
FROM    [File] a
        INNER JOIN
        (
            SELECT  hash
            FROM    [File] b
            GROUP   BY hash
            HAVING  COUNT(*) > 1
        ) b ON  a.hash = b.hash
于 2013-05-22T09:12:48.540 回答