+ -
当前位置:首页 → 问答吧 → 一个sql查询问题

一个sql查询问题

时间: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 num
  from (
  select distinct fstuid as num
  from tgrade
  union all
  select distinct scourseid as num from tgrade)

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

SELECT num
  from (
  select distinct fstuid as num
  from tgrade
  UNION
  select distinct scourseid as num from tgrade)
  ORDER BY NUM;

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

注意一下union 和 union all 的区别
union all不会去重复行

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