使用不同的运算符重复

时间:2018-12-29 16:04:47

标签: sql-server-2012 distinct-values

我有下表。

create table html_details(A int,b int,c int,d nvarchar(4))

create table pdf_details(A int,b int,c int,d nvarchar(4))

insert into pdf_details values(1,2,3,'pdf')
insert into pdf_details values(1,2,3,'pdf')
insert into pdf_details values(4,5,6,'pdf')

insert into html_details values(1,2,3,'html')
insert into html_details values(1,2,3,'html')
insert into html_details values(4,5,6,'html')

现在我正在使用以下查询来避免每个表中的重复。

select distinct a,b,c,d from html_details
union all
select distinct a,b,c,d from pdf_details

但是上面的查询由于两个查询的功能不同而导致性能不佳。所以我在外部查询中使用了distinct.Now性能有所提高,但会给出相同的输出吗?两个查询的逻辑都相同吗?

select distinct a,b,c,d from (
select  a,b,c,d from html_details
union all
select a,b,c,d from pdf_details
)a

1 个答案:

答案 0 :(得分:1)

不。它将不会返回相同的输出。

在各个查询中的区别将使您从两个查询中获得唯一记录,然后将其合并。因此,如果两个查询结果中都有相似的行,则它们都将出现在最终结果中。

让我们说您的数据是:

表1:

1,2,3,pdf 1,2,3,pdf 1,2,3,hello

表2:

1,2,3,html 1,2,3,html 1,2,3,hello

第一种方法的结果将是(最终响应没有明显不同)-

1,2,3,pdf 1,2,3,hello 1,2,3,html 1,2,3,hello

第二种方法的结果将是(最终响应有明显不同)-

1,2,3,pdf 1,2,3,html 1,2,3,hello

我希望这能解释。

相关问题