说明
本文章整理了47道常见sql联系题,包括建表语句,表结构,习题列表,解题答案都涵盖在本文章内。文末提供了所用SQL脚本下载链接。所有解题答案都是本人自己写的,广大读者如果在阅读使用中,有任何问题欢迎留言,对我写的有问题的,欢迎指正,谢谢。
数据库:oracle11g
表结构
可使用plsql-工具-导入表,导入如下sql脚本。
学生表
Student(SId,Sname,Sage,Ssex)
SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
create table Student(SId varchar(10),Sname varchar(10),Sage date,Ssex varchar(10));
insert into Student values('01' , '赵雷' , to_date('1990-01-01','yyyy-mm-dd'), '男');
insert into Student values('02' , '钱电' , to_date('1990-12-21','yyyy-mm-dd') , '男');
insert into Student values('03' , '孙风' , to_date('1990-12-20','yyyy-mm-dd') , '男');
insert into Student values('04' , '李云' , to_date('1990-12-06','yyyy-mm-dd') , '男');
insert into Student values('05' , '周梅' , to_date('1991-12-01','yyyy-mm-dd') , '女');
insert into Student values('06' , '吴兰' , to_date('1992-01-01','yyyy-mm-dd') , '女');
insert into Student values('07' , '郑竹' , to_date('1989-01-01','yyyy-mm-dd') , '女');
insert into Student values('09' , '张三' , to_date('2017-12-20','yyyy-mm-dd') , '女');
insert into Student values('10' , '李四' , to_date('2017-12-25','yyyy-mm-dd') , '女');
insert into Student values('11' , '李四' , to_date('2012-06-06','yyyy-mm-dd') , '女');
insert into Student values('12' , '赵六' , to_date('2013-06-13','yyyy-mm-dd') , '女');
insert into Student values('13' , '孙七' , to_date('2014-06-01','yyyy-mm-dd') , '女');
课程表
Course(CId,Cname,TId)
CId 课程编号,Cname 课程名称,TId 教师编号
create table Course(CId varchar(10),Cname varchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
教师表
Teacher(TId,Tname)
TId 教师编号,Tname 教师姓名
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
成绩表
SC(SId,CId,score)
SId 学生编号,CId 课程编号,score 分数
create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
练习题
1 查询 01 课程比 02 课程成绩高的学生的信息及课程分数
2 查询同时存在" 01 "课程和" 02 "课程的情况
3 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
4 查询不存在" 01 "课程但存在" 02 "课程的情况
5 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
6 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
7 查有成绩的学生信息
8 查询「李」姓老师的数量
9 查询「李」姓老师的数量
10 查询没有学全所有课程的同学的信息
11 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
12 查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
13 查询没学过"张三"老师讲授的任一门课程的学生姓名
14 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
15 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
16 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
17 查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
18 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
19 查询学生的总成绩,并进行排名,总分重复时保留名次空缺
20 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
21 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
22 查询各科成绩前三名的记录
21 查询每门课程被选修的学生数
22 查询出只选修两门课程的学生学号和姓名
23 查询男生、女生人数
24 查询名字中含有「风」字的学生信息
25 查询同名同性学生名单,并统计同名人数
26 查询 1990 年出生的学生名单
27 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
28 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
29 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
30 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
31 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
32 查询不及格的课程
33 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
34 求每门课程的学生人数
35 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
36 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
37 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
38 查询每门功成绩最好的前两名
39 统计每门课程的学生选修人数(超过 5 人的课程才统计)
40 检索至少选修两门课程的学生学号
41 查询选修了全部课程的学生信息
42 查询各学生的年龄,只按年份来算
43 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
44 查询本周过生日的学生
45 查询下周过生日的学生
46 查询本月过生日的学生
47 查询下月过生日的学生
解题答案
----查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
SELECT ST.SID SID,ST.SNAME NAME,S1.SCORE SCORE1,S2.SCORE SCORE2
FROM (SELECT * FROM SC WHERE CID = '01') S1,
(SELECT * FROM SC WHERE CID = '02') S2,
STUDENT ST
WHERE S1.SID = S2.SID
AND S1.SCORE > S2.SCORE
AND S1.SID = ST.SID;
---1.1 查询同时存在" 01 "课程和" 02 "课程的情况
SELECT ST.SID SID,ST.SNAME NAME,S1.SCORE SCORE1,S2.SCORE SCORE2
FROM (SELECT * FROM SC WHERE CID = '01') S1,
(SELECT * FROM SC WHERE CID = '02') S2,
STUDENT ST
WHERE S1.SID = S2.SID
AND S1.SID = ST.SID;
----1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 NULL )
SELECT *
FROM (SELECT * FROM SC WHERE SC.CID = '01') SC1
LEFT JOIN
(SELECT * FROM SC WHERE SC.CID = '02') SC2
ON SC1.SID = SC2.SID;
----1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
SELECT *
FROM SC
WHERE SC.SID NOT IN
(SELECT SID FROM SC WHERE SC.CID = '01')
AND SC.CID = '02';
----2查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT * FROM (SELECT SC.SID SSID,ST.SNAME NAME,AVG(SC.SCORE) AV
FROM STUDENT ST,SC
WHERE ST.SID = SC.SID
GROUP BY SC.SID ,ST.SNAME
) RS
WHERE RS.AV >= 60
ORDER BY RS.AV;
---查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 NULL )
SELECT ST.SID,ST.SNAME,SCC.CIDS,SCC.SCORES
FROM STUDENT ST LEFT JOIN
(SELECT SC.SID SSID,ST.SNAME NAME,COUNT(SC.CID) CIDS,SUM(SC.SCORE) SCORES
FROM STUDENT ST,SC
WHERE ST.SID = SC.SID
GROUP BY SC.SID ,ST.SNAME) SCC
ON ST.SID = SCC.SSID;
-- 查有成绩的学生信息
SELECT SC.SID SSID,ST.SNAME NAME,COUNT(SC.CID) CIDS,SUM(SC.SCORE) SUM
FROM STUDENT ST,SC
WHERE ST.SID = SC.SID
GROUP BY SC.SID ,ST.SNAME;
--查询「李」姓老师的数量
SELECT COUNT(*) FROM TEACHER TR WHERE TR.TNAME LIKE '李%' ;
--查询学过「张三」老师授课的同学的信息
SELECT ST.SID,ST.SNAME
FROM TEACHER TR,SC,STUDENT ST,COURSE CS
WHERE TR.TNAME = '张三'
AND TR.TID = CS.TID
AND CS.CID = SC.CID
AND ST.SID = SC.SID;
--查询没有学全所有课程的同学的信息
SELECT ST.SID,ST.SNAME FROM STUDENT ST
WHERE ST.SID NOT IN(
SELECT SID FROM(
SELECT SID,COUNT(CID) CIDS
FROM SC
GROUP BY SID) NL
WHERE NL.CIDS = (SELECT COUNT(CID) FROM COURSE));
--查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
SELECT * FROM STUDENT WHERE SID IN(
SELECT DISTINCT SID FROM SC WHERE CID IN(
SELECT SC.CID FROM SC WHERE SC.SID = '01'));
--查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
WITH B AS (SELECT DISTINCT CID AS DISTINCT_CID FROM SC WHERE SID = '01')
SELECT * FROM STUDENT
WHERE SID IN
(
SELECT SC.SID FROM SC LEFT JOIN B
ON B.DISTINCT_CID = SC.CID
GROUP BY SC.SID
HAVING COUNT(SC.CID) = (SELECT COUNT(DISTINCT_CID) FROM B )
AND COUNT(B.DISTINCT_CID) = (SELECT COUNT(DISTINCT_CID) FROM B )
);
--查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT ST.SNAME,ST.SID FROM STUDENT ST
WHERE ST.SID NOT IN (SELECT SC.SID SSID FROM TEACHER TR,COURSE CS,SC
WHERE TR.TNAME = '张三'
AND TR.TID = CS.TID
AND CS.CID = SC.CID);
--查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
WITH B AS (SELECT DISTINCT SID,COUNT(SID) NUMS FROM SC WHERE SC.SCORE < 60 GROUP BY SID)
SELECT ST.* FROM STUDENT ST RIGHT JOIN B
ON ST.SID = B.SID
AND B.NUMS >1;
--检索" 01 "课程分数小于 60,按分数降序排列的学生信息
SELECT ST.*,SC.SCORE FROM SC,STUDENT ST
WHERE SC.CID = '01'
AND SC.SCORE < 60
AND ST.SID = SC.SID
ORDER BY SCORE DESC;
--按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
WITH B AS (SELECT DISTINCT SID DISTINCT_SID,AVG(SCORE) AVGSCORE FROM SC GROUP BY SID)
SELECT SID,CID, SCORE,B.AVGSCORE
FROM SC LEFT JOIN B
ON B.DISTINCT_SID = SC.SID
ORDER BY B.AVGSCORE DESC,CID ASC,SID ASC;
--查询各科成绩最高分、最低分和平均分:
--以如下形式显示:课程 ID,课程 NAME,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT DISTINCT SC.CID DISTINCT_CID,CS.CNAME,COUNT(CS.CID) NUMS,MAX(SCORE) MAX_SCORE,MIN(SCORE) MIN_SCORE,ROUND(AVG(SCORE),2) AVG_SCORE,
ROUND(100*SUM(CASE WHEN SC.SCORE>=60 THEN 1 ELSE 0 END)/COUNT(CS.CID),2)||'%' AS 及格率,
ROUND(100*SUM(CASE WHEN SC.SCORE>=70 AND SC.SCORE <80 THEN 1 ELSE 0 END)/COUNT(CS.CID),2)||'%' AS 中等率,
ROUND(100*SUM(CASE WHEN SC.SCORE>=80 AND SC.SCORE <90 THEN 1 ELSE 0 END)/COUNT(CS.CID),2)||'%' AS 优良率,
ROUND(100*SUM(CASE WHEN SC.SCORE>=90 THEN 1 ELSE 0 END)/COUNT(CS.CID),2)||'%' AS 优秀率
FROM SC,COURSE CS
WHERE SC.CID = CS.CID
GROUP BY SC.CID,CS.CNAME
ORDER BY NUMS DESC,DISTINCT_CID ASC;
--按各科成绩进行排序,并显示排名, SCORE 重复时保留名次空缺
SELECT CID,SID,SCORE, RANK() OVER(PARTITION BY CID ORDER BY SCORE DESC) RANK
FROM SC
GROUP BY CID,SID,SCORE
ORDER BY CID,SCORE DESC,RANK;
--按各科成绩进行排序,并显示排名, SCORE 重复时合并名次
SELECT CID,SID,SCORE, ROW_NUMBER() OVER(PARTITION BY CID ORDER BY SCORE DESC) RANK
FROM SC
GROUP BY CID,SID,SCORE
ORDER BY CID,SCORE DESC,RANK;
--查询学生的总成绩,并进行排名,总分重复时保留名次空缺
SELECT DISTINCT A.SID ID,SUM(A.SCORE) ZF,ROW_NUMBER() OVER(ORDER BY SUM(A.SCORE) DESC) RANK
FROM SC A
GROUP BY SID
ORDER BY SUM(SCORE) DESC;
--查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
SELECT DISTINCT A.SID ID,SUM(A.SCORE) ZF,RANK() OVER(ORDER BY SUM(A.SCORE) DESC) RANK
FROM SC A
GROUP BY SID
ORDER BY SUM(SCORE) DESC;
--统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
SELECT SC.CID,CS.CNAME,
SUM((CASE WHEN SC.SCORE>=85 THEN 1 ELSE 0 END)) QJ1,
ROUND(SUM((CASE WHEN SC.SCORE>=85 THEN 1 ELSE 0 END))/SUM(SC.SCORE)*100,2)||'%' QJ1L,
SUM((CASE WHEN SC.SCORE<85 AND SC.SCORE >=70 THEN 1 ELSE 0 END)) QJ2,
ROUND(SUM((CASE WHEN SC.SCORE<85 AND SC.SCORE >=70 THEN 1 ELSE 0 END))/SUM(SC.SCORE)*100,2)||'%' QJ2L,
SUM((CASE WHEN SC.SCORE<70 AND SC.SCORE >=60 THEN 1 ELSE 0 END)) QJ3,
ROUND(SUM((CASE WHEN SC.SCORE<70 AND SC.SCORE >=60 THEN 1 ELSE 0 END))/SUM(SC.SCORE)*100,2)||'%' QJ3L,
SUM((CASE WHEN SC.SCORE<60 THEN 1 ELSE 0 END)) QJ4,
ROUND(SUM((CASE WHEN SC.SCORE<60 THEN 1 ELSE 0 END))/SUM(SC.SCORE)*100,2)||'%' QJ4L
FROM COURSE CS,SC
WHERE SC.CID = CS.CID
GROUP BY SC.CID,CS.CNAME;
--查询各科成绩前三名的记录
SELECT *
FROM (SELECT CID,SID,SCORE, RANK() OVER(PARTITION BY CID ORDER BY SCORE DESC) RANK
FROM SC
GROUP BY CID,SID,SCORE
ORDER BY CID,SCORE DESC,RANK) B
WHERE B.RANK <=3;
--查询每门课程被选修的学生数
SELECT SC.CID,CS.CNAME,COUNT(SC.SID)
FROM SC,COURSE CS
WHERE SC.CID = CS.CID
GROUP BY SC.CID,CS.CNAME;
--查询出只选修两门课程的学生学号和姓名
SELECT * FROM (
SELECT SC.SID,ST.SNAME,COUNT(SC.SID) NUMS
FROM SC,STUDENT ST
WHERE ST.SID = SC.SID
GROUP BY SC.SID,ST.SNAME) B
WHERE B.NUMS=2;
--查询男生、女生人数
SELECT SSEX,SUM(SID)
FROM STUDENT ST
GROUP BY SSEX;
--查询名字中含有「风」字的学生信息
SELECT * FROM STUDENT WHERE SNAME LIKE '%风%';
--查询同名同性学生名单,并统计同名人数
SELECT T1.SSEX,T1.SNAME,COUNT(T1.SID)
FROM STUDENT T1,STUDENT T2
WHERE T1.SNAME = T2.SNAME
AND T1.SSEX = T2.SSEX
AND T1.SID != T2.SID
GROUP BY T1.SSEX,T1.SNAME;
--查询 1990 年出生的学生名单
SELECT * FROM STUDENT WHERE TO_CHAR(SAGE,'YYYY/MM/DD') LIKE '1990%';
--查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT SID,AVG(SCORE) AVERAGE FROM SC GROUP BY SID ORDER BY AVERAGE DESC,SID;
--查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
SELECT ST.SID,ST.SNAME,T.AVERAGE
FROM STUDENT ST ,(
SELECT SID,AVG(SCORE) AVERAGE FROM SC GROUP BY SID) T
WHERE T.AVERAGE >=85
AND T.SID = ST.SID;
--查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
SELECT ST.SID,ST.SNAME,SC.CID,SC.SCORE
FROM COURSE CS,STUDENT ST,SC
WHERE CS.CNAME = '数学'
AND ST.SID = SC.SID
AND SC.CID = CS.CID
AND SC.SCORE < 60;
--查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
SELECT ST.SID,SC.CID,SC.SCORE
FROM SC RIGHT JOIN STUDENT ST
ON ST.SID = SC.SID;
--查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
SELECT SC.SID,CS.CNAME,SC.SCORE
FROM SC,COURSE CS,(SELECT SID,MIN(SCORE) MIN_SCORE FROM SC GROUP BY SID) A
WHERE SC.SID =A.SID
AND SC.CID = CS.CID
AND A.MIN_SCORE >=70
ORDER BY SC.SID;
--查询不及格的课程
SELECT SC.SID,SC.CID,CS.CNAME,SC.SCORE
FROM COURSE CS , SC
WHERE SC.CID = CS.CID
AND SC.SCORE < 60;
--查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
SELECT ST.SID,ST.SNAME,SC.CID,SC.SCORE
FROM STUDENT ST,SC
WHERE SC.CID = '01'
AND ST.SID = SC.SID
AND SC.SCORE >= 80;
--求每门课程的学生人数
SELECT SC.CID,COUNT(SC.CID)
FROM COURSE CS LEFT JOIN SC
ON CS.CID = SC.CID
GROUP BY SC.CID;
--成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT ST.*,SC.SCORE
FROM TEACHER TR,STUDENT ST,COURSE CS,SC
WHERE SC.CID=CS.CID
AND TR.TID = CS.TID
AND ST.SID = SC.SID
AND TR.TNAME = '张三'
AND ROWNUM = 1
ORDER BY SC.SCORE DESC;
--成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT * FROM
(SELECT T.SSID,T.NAME,T.SR, RANK() OVER(ORDER BY T.SR DESC) RANK
FROM
(SELECT ST.SID SSID,ST.SNAME NAME,SC.SCORE SR
FROM TEACHER TR,STUDENT ST,COURSE CS,SC
WHERE SC.CID=CS.CID
AND TR.TID = CS.TID
AND ST.SID = SC.SID
AND TR.TNAME = '张三') T)
WHERE RANK = 1;
--查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT SC1.SID,SC1.CID,SC1.SCORE
FROM SC SC1 , SC SC2
WHERE SC1.SCORE = SC2.SCORE
AND SC1.SID = SC2.SID
AND SC1.CID != SC2.CID;
--查询每门功成绩最好的前两名
SELECT *
FROM(SELECT SC.CID,SC.SID,SC.SCORE,RANK() OVER(PARTITION BY SC.CID ORDER BY SC.SCORE DESC) RANK FROM SC ) T
WHERE T.RANK <=2;
--统计每门课程的学生选修人数(超过 5 人的课程才统计)。
SELECT * FROM
(SELECT SC.CID,COUNT(SC.SID) NUMS FROM SC GROUP BY SC.CID) T
WHERE T.NUMS>=5;
--检索至少选修两门课程的学生学号
SELECT * FROM
(SELECT SC.SID,COUNT(SC.CID) NUMS FROM SC GROUP BY SC.SID) T
WHERE T.NUMS >=2;
--查询选修了全部课程的学生信息
SELECT ST.* FROM
(SELECT SC.SID SSID,COUNT(SC.CID) NUMS FROM SC GROUP BY SC.SID) T,
STUDENT ST
WHERE T.NUMS = (SELECT COUNT(CID) FROM COURSE)
AND ST.SID = T.SSID;
--查询各学生的年龄,只按年份来算
SELECT ST.*,(SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL)-SUBSTR(TO_CHAR(ST.SAGE,'YYYY/MM/DD'),0,4) AGE
FROM STUDENT ST;
--按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT ST.*,
(SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL)-SUBSTR(TO_CHAR(ST.SAGE,'YYYY/MM/DD'),0,4)
-(CASE WHEN TO_CHAR(SYSDATE,'MMDD') > TO_CHAR(ST.SAGE,'MMDD') THEN 1 ELSE 0 END ) AGE
FROM STUDENT ST;
--查询本周过生日的学生
WITH T AS (SELECT TO_CHAR(TRUNC(NEXT_DAY(SYSDATE-8,1)+1)+ROWNUM-1,'YYYYMMDD') DAYS
FROM DUAL
CONNECT BY ROWNUM<=TRUNC(TRUNC(NEXT_DAY(SYSDATE-8,1)+7)+1-TRUNC(NEXT_DAY(SYSDATE-8,1)+1)))
SELECT ST.* FROM STUDENT ST
WHERE TO_CHAR(ST.SAGE,'MMDD') IN (SELECT SUBSTR(T.DAYS,5,4) FROM T);
--查询下周过生日的学生
WITH T AS (SELECT TO_CHAR(TRUNC(NEXT_DAY(SYSDATE-8,1)+8)+ROWNUM-1,'YYYYMMDD') DAYS
FROM DUAL
CONNECT BY ROWNUM<=TRUNC(TRUNC(NEXT_DAY(SYSDATE-8,1)+14)+1-TRUNC(NEXT_DAY(SYSDATE-8,1)+8)))
SELECT ST.* FROM STUDENT ST
WHERE TO_CHAR(ST.SAGE,'MMDD') IN (SELECT SUBSTR(T.DAYS,5,4) FROM T);
--查询本月过生日的学生
SELECT ST.* FROM STUDENT ST
WHERE SUBSTR(TO_CHAR(ST.SAGE,'YYYYMMDD'),5,2) = SUBSTR(TO_CHAR(SYSDATE,'YYYYMMDD'),5,2);
--查询下月过生日的学生
SELECT ST.* FROM STUDENT ST
WHERE SUBSTR(TO_CHAR(ST.SAGE,'YYYYMMDD'),5,2) = SUBSTR(TO_CHAR(ADD_MONTHS(SYSDATE,1),'YYYYMMDD'),5,2);
sql脚本下载
原文出处:https://www.cnblogs.com/happy-sharing/p/12152999.html
来源:oschina
链接:https://my.oschina.net/u/4407134/blog/3242803