Sql 为什么select语句从有重复结果的表返回不同的值?

Sql 为什么select语句从有重复结果的表返回不同的值?,sql,sql-server,Sql,Sql Server,我正在这样的表中插入值(SQL Server) 但它给了我这些数据: id ----- 1 2 虽然我需要以下数据: id ---- 1 2 2 1 使用union all代替union全部联合允许选择冗余数据: create table #t1 (id int) insert into #t1(id) select 1 union all select 2 union all select 2 union all select 1 我只是一个接一个地回

我正在这样的表中插入值(SQL Server)

但它给了我这些数据:

  id
-----
  1
  2
虽然我需要以下数据:

  id
----
  1
  2
  2
  1

使用
union all
代替
union
<代码>全部联合允许选择冗余数据:

create table #t1 (id int)

insert into #t1(id)
select 1 union all
select 2 union all
select 2 union all
select 1 

我只是一个接一个地回答


UNION
始终返回不同的行,否则请使用
UNION ALL

设置问题的格式。因为您使用的UNION意味着差异,因此会删除重复的条目
create table #t1 (id int)

insert into #t1(id)
select 1 union all
select 2 union all
select 2 union all
select 1