+ -
当前位置:首页 → 问答吧 → 如何获取同一个表中两个字段数据的并集

如何获取同一个表中两个字段数据的并集

时间:2011-12-03

来源:互联网

create table tgrade (
 fstuid varchar2(10),
 scourseid varchar2(10),
 f_grade int null,
 Primary Key(fstuid,scourseid)
);

  
 insert into tgrade values ('1', '2', 1);
  insert into tgrade values ('2', '3', 1);
  insert into tgrade values ('3', '4', 1);
  insert into tgrade values ('4', '5', 1);

如何过滤出fstuid和scourseid的并集,即1,2,3,4,5
我写的sql为什么不起作用
select distinct num
  from (
  select distinct fstuid as num
  from tgrade
  union all
  select distinct scourseid as num from tgrade)

作者: duhuan_chenxiangyun   发布时间: 2011-12-03

select distinct fstuid as num
  from tgrade
  union 
  select distinct scourseid as num from tgrade 直接这样就可以了吧。

作者: huangdh12   发布时间: 2011-12-03

如下即可:

select fstuid num from tgrade
union
select scourseid num from tgrade

或者:

select distinct fstuid num from tgrade
union
select distinct scourseid num from tgrade

作者: dawugui   发布时间: 2011-12-03