BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / database / #3209同步于 2009/4/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Database机器人发帖

sql中除怎么操作[求助]

chelseacx
2009/4/8镜像同步9 回复
具体的情形是学生选课系统 查找至少选修了A同学所选的所有课程的所有同学 应该是用除吧[em17] 拜谢拜谢!
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
yanhui机器人#1 · 2009/4/9
select * from 表1 where 学生id in ( select 学生id from 表2 where 课程名 in (select 课程名 from 表2 where 学生id = A同学) ) 这样写对吗? 怎么看着这sql句子感觉好不爽。。。。
chelseacx机器人#2 · 2009/4/9
【 在 yanhui 的大作中提到: 】 : select * from 表1 where 学生id : in : ( : ................... 我弄进去实验下,一会答复[em1]。。
chelseacx机器人#3 · 2009/4/9
【 在 yanhui 的大作中提到: 】 : select * from 表1 where 学生id : in : ( : ................... 有点问题,这个查出来的是任何选修了有一门和A同学选课相同的课程的其他学生 要求的是至少选修了所有A同学选的课程。。。 所以感觉要用除。。
Aieu机器人#4 · 2009/4/9
占坑等达人,用计数+嵌套循环的笨办法抛块砖先~实际运行还要考虑几点: 1.符合条件的学生名用dbms_output输出,如用户数较多会溢出,建议另建表保存; 2.由于没有经过实际调试,如果运行时报错,可以直接把注释部分删掉,同时检查下标点。 [quote] /*假设单表结构(表结构如下所示),A所选课程数不定: col_1(学生名) varchar col_2(课程名) varchar table_Name */ declare a_cnt int; /*记录学生A所选课程数*/ b_cnt int; /*b_cnt用于记录扫描到的特定学生所选课程中,与A所选课程相同数目*/ tmp_cnt int; /*临时计数器*/ begin select count(1) into a_cnt from table_name where col_1 = 'A'; --选出除A外的其他学生依次进行判断 for s in (select distinct col_1 from table_name where col_1 <> 'A') loop b_cnt := 0; for rec in (select * from table_name where col_1 = 'A') loop tmp_cnt := 0; --如外层循环中的学生选的课程与当前扫描到的A所选课程 相同,相同课程数b_cnt+1 select count(1) into tmp_cnt from table_name t where t.col_1 = s.col_1 and t.col_2 = rec.col_2; if (tmp_cnt > 0) then b_cnt := b_cnt + 1; end if; end loop; --若与学生A所选课程数相同,返回该学生姓名 if (b_cnt = a_cnt) then dbms_output.put_line(s.col_1); end if; end loop; end; [/quote]
lixiao7755机器人#5 · 2009/4/10
select distinct S.sno from sc as S where not exists ( ( select cno from sc where sno='030219') except ( select T.cno from sc as T where S.sno=T.sno ))
lixiao7755机器人#6 · 2009/4/10
郁闷的是sybase中不能用except 。kingbase好像行,没试过。
chelseacx机器人#7 · 2009/4/10
【 在 lixiao7755 的大作中提到: 】 : select distinct S.sno : from sc as S : where not exists ( ( select cno : ................... 其实except完全可以用not in 代替...
chelseacx机器人#8 · 2009/4/10
【 在 Aieu 的大作中提到: 】 : 占坑等达人,用计数+嵌套循环的笨办法抛块砖先~实际运行还要考虑几点: : 1.符合条件的学生名用dbms_output输出,如用户数较多会溢出,建议另建表保存; : 2.由于没有经过实际调试,如果运行时报错,可以直接把注释部分删掉,同时检查下标点。 : ................... 再小问下,SQL中是没有类似关系代数除的操作吗?
Aieu机器人#9 · 2009/4/19
oracle有minus,不过两个集合值相等的话,结果是什么类型的不清楚 【 在 chelseacx 的大作中提到: 】 : 再小问下,SQL中是没有类似关系代数除的操作吗?