我试图运行一个查询,不知道为什么返回的记录集关闭了60行。
Select t1.* into #temp1
from NameExt as t1
join #temp1 as t2
on t1.AN = t2.AN --164172
Select t1.* into #temp3
from NameExt as t1
where AN in (Select AN from #temp1) --164112
当我在#temp1和#temp2上进行交叉或除外时,我没有得到任何行。
真的需要理解为什么两个查询返回的不是类似的记录集。
即使这返回与第二个查询相同的行数
Select * into #temp3 from NameExt as t1 where exists
( Select 1 from #temp1 as t2 where t1.AN = t2.AN) --164112
非常感谢
通过执行以下操作,您可以轻松查看导致问题的值:
select t2.AN
from #temp1 t2
group by t2.AN
having count(*) > 1;
第二个表中的重复项导致问题。你知道如何解决它。 。 。使用in
或exists
。
您的查询不正确。您无法同时从中选择和创建临时表。
从NameExt中选择t1。*到#temp1作为t1连接#temp1为t1上的t2AN = t2.AN --164172
根据评论,你可能在#temp1
中有多行,AN
中的值相同...
CREATE TABLE x (
id INT
)
CREATE TABLE y (
id INT,
x_id INT
)
INSERT INTO x VALUES (1), (2), (3)
INSERT INTO y VALUES (1, 2), (2, 2), (3, 3)
SELECT *
FROM x INNER JOIN y ON x.id = y.x_id
-- a total of three rows
-- x.id | y.id | y.x_id
------------------------
-- 2 | 1 | 2
-- 2 | 2 | 2
-- 3 | 3 | 3
SELECT *
FROM x
WHERE x.id IN (SELECT y.x_id FROM y)
-- a total of two rows
-- x.id
--------
-- 2
-- 3