假设我有个表T, 里面的样本数据如下:
student school
--------------------
学生1 初中1
学生1 高中1
学生1 大学1
学生2 高中2
学生2 大学2
学生3 高中1
学生3 大学2
学生4 高中1
学生5 大学2我想从表T里面找出同时满足下面条件的数据:
cond1:上过多个学校的学生的记录,这样 学生4 和 学生5 不满足条件
cond2:上过 高中1 的学生的数据 , 这样 就排除 学生3 的数据了总之,我期望的结果是:
student school
--------------------
学生1 初中1
学生1 高中1
学生1 大学1
学生3 高中1
学生3 大学2
student school
--------------------
学生1 初中1
学生1 高中1
学生1 大学1
学生2 高中2
学生2 大学2
学生3 高中1
学生3 大学2
学生4 高中1
学生5 大学2我想从表T里面找出同时满足下面条件的数据:
cond1:上过多个学校的学生的记录,这样 学生4 和 学生5 不满足条件
cond2:上过 高中1 的学生的数据 , 这样 就排除 学生3 的数据了总之,我期望的结果是:
student school
--------------------
学生1 初中1
学生1 高中1
学生1 大学1
学生3 高中1
学生3 大学2
解决方案 »
- 一个ORACLE EXPDP 问题,要崩溃了,请高手帮忙看看
- PL/SQL:ORA-06552错误
- 求oracle 下载地址!
- 哪里有一个完整的SQL和PL/SQL关键字列表丫?
- 一条奇怪的SQL语句,在DatabaseTour 里执行的结果和在 VB应用程序里执行的结果不一样!!(急用)
- 在oracle中可有类似sqlsever sp_columns的存储过程???
- 过程或函数在调用时,如何使用默认参数?
- 知道系统进程SID号,如何将其杀掉?很急,谢谢
- 如何设置remote_login_passwordfile为exclusive
- 请各位大神帮帮忙,oracle 12C版本导出dmp文件无法导入11G版本
- 将20080800转成20090900有没有什么好办法?
- Oracle中主从多表删除数据时,必须用级联删除吗?
cond1条件的sql如下:
select student,school,count(1) from t group by studnet,school having count(1)>1;
create table t(student varchar2(8),school varchar2(8));
select * from t;
student school
--------------------
学生1 初中1
学生1 高中1
学生1 大学1
学生2 高中2
学生2 大学2
学生3 高中1
学生3 大学2
学生4 高中1
学生5 大学2
select *
from t
where student in
(select student
from t
where student in
(select student from t group by student having count(1) > 1)
and school = '高中1');
STUDENT SCHOOL
1 学生1 初中1
2 学生1 高中1
3 学生1 大学1
4 学生3 高中1
5 学生3 大学2
--还可以这样写
select *
from t
where student in (select student
from t t1
where exists (select 1
from t t2
where t1.student = t2.student
and t2.school = '高中1')
group by student
having count(1) > 1)
--运行结果:
1 学生1 初中1
2 学生1 高中1
3 学生1 大学1
4 学生3 高中1
5 学生3 大学2
where student in (
select student from xxx
having count(*) > 1
group by student
intersect
select student from xxx
where school = '高中1')
--稍有点复杂,有空再想想怎么简化
select c.* from
stu_info_tab c,
(
select student
from stu_info_tab a
where exists (select 1 from stu_info_tab b where a.student = b.student and school='高中1')
group by student having count(school) > 1
)d
where c.student = d.student;
--这个好理解一点
select c.* from
(
--cond2
select * from stu_info_tab a
where exists (select 1 from stu_info_tab b where a.student = b.student and school='高中1')
)c,
(
--cond1
select student
from stu_info_tab a
group by student having count(school) > 1
)d
where c.student = d.student;
select student from t group by student having count(1) > 1
intersect
select student from t where school = '高中1' )