使用不同的运算符重复

Duplicates using distinct operator

我有以下表格。

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

但是上面的查询性能很差,因为两者都有不同的功能 query.so 我在外部使用不同的 query.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

没有。它不会return相同的输出。

Distinct in individual queries 将从两个查询中获得唯一的记录,然后它将被合并。所以如果两个查询结果中有相似的行,它们都会出现在最终结果中。

假设您的数据是:

Table 1:

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

Table 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

我希望这能解释清楚。