SQL集合查询,20. 使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集;

2025-03-05 07:00:56
推荐回答(4个)
回答1:

很高兴回答你的问题

根据你的要求,改后SQL为:

select sname,sage,decode(s,'1',sname) name,decode(s,'1',sage) age from(

select sname,sage,sum(s) s from(

select Sname,Sage,'0' s from student where Sdept='CS'

union

select Sname,Sage,'0' s from student where Sage<=19

union

select Sname,Sage,'1' s from student where Sdept='CS'

minus

select Sname,Sage,'1' s from student where Sage<=19

)

group by sname,sage)

该SQL是在ORACLE数据库当中执行,当中的minus和你的except作用是一样的,请看

效果图(图中数据为测试数据)

如有不懂请Hi我,扣我250267873都行

回答2:

select Sname,Sage from student where Sdept='CS' and Sage<=19
union
select Sname,Sage from student where Sdept='CS'
except Sage<=19
下午手机上的,直接发上去了。。。

回答3:

还是应该分开两个结果显示好一些,因为两个结果的行数可能不一样,在一个结果中按四列显示可能不太合适。

回答4:

我怎么都看不懂?