我有一个大约400,000行的庞大数据集。我想只选择同一表中第三列(val
)中存在第二列(ecr
)值的行。
例如,在下面显示的示例屏幕截图中,第二行( 4294939057 )上的列val
的值等于列ecr
的第三行值。
我尝试使用以下查询,但它似乎没有给出正确的行。
非常感谢任何提示。
use dbTest
select val, ecr
from tableTest
group by val
having COUNT (val) > 1
我正在使用SQL Server 2008。
答案 0 :(得分:3)
你应该使用自联接(由于你的表的维度,你可能需要在val和ecr上使用适当的分离索引)
select a.*, b.*
from tableTest as a
inner join tableTest as b
on a.val = b.ecr
答案 1 :(得分:2)
如果您不想要内部联接的完整输出,可以使用以下内容:
select *
from tableTest as t
where exists (
select 1
from tableTest as i
where t.val = i.ecr
)
答案 2 :(得分:1)
除了连接之外的另一个选项是子查询:
SELECT *
FROM tableTest
WHERE val IN (SELECT ecr FROM tableTest)