[SQL28] Query information and course scores of students with higher grades in course 1 than course 2

There are several tables as follows:

Student
sid    sname      sage              sex
1      '赵雷'     '1990-01-01'      '男'
2      '钱电'     '1990-01-01'      '男'
3      '孙风'     '1990-01-01'      '男'
4      '李云'     '1990-01-01'      '男'
5      '周梅'     '1990-01-01'      '女'
6      '吴兰'     '1990-01-01'      '女'
7      '郑竹'     '1990-01-01'      '女'
8      '王菊'     '1990-01-01'      '女'
course
cid     cname       tid
1       '语文'      2
2       '数学'      1
3       '英语'      3
shc
sid     cid     score
1       1       80
1       2       90
1       3       99
2       1       70
2       2       60
2       3       80
3       1       80
3       2       80
3       3       80
4       1       50
4       2       30
4       3       20
5       1       76
5       2       87
6       1       31
6       3       34
7       2       89
7       3       98

Inquire about the information and course scores of students with higher grades in course 1 than course 2?


solve:

SELECT *
  from (
        SELECT a.SId,a.score class1,b.score class2
          FROM (SELECT * from sc WHERE sc.CId = 1)as a
         inner join (SELECT * from sc WHERE sc.CId = 2)as b
            on a.SId = b.SId
         WHERE a.score > b.score
        )r
  left join student a
ON a.SId = r.SId
;
sid     class1  class2  sid(1)  sname     sage                    sex
2       70      60      2       钱电      1990-01-01 00:00:00     男
4       50      30      4       李云      1990-01-01 00:00:00     男

Remarks: create table and data
create table student(sid int,sname varchar(10),sage datetime,sex varchar(10));
insert into student values(1,'Zhao Lei', '1990-01-01', ' Male');
insert into student values(2,'钱电', '1990-01-01','男');
insert into student values(3,'Sun Feng', '1990-01-01', ' Male');
insert into student values(4,'李云', '1990-01-01','男');
insert into student values(5,'周梅', '1990-01-01', ' Female');
insert into student values(6,'吴兰', '1990-01-01','女');
insert into student values(7,'郑竹', '1990-01-01', ' Female');
insert into student values(8,'王菊', '1990-01-01','女');

create table course(cid int,cname varchar(10),tid int);
insert into course values(1, '语文', 2);
insert into course values(2, '数学', 1);
insert into course values(3, '英语', 3);

create table shc(sid int,cid int,score int);
insert into shc values(1, 1, 80);
insert into shc values(1, 2, 90);
insert into shc values(1, 3, 99);
insert into shc values(2, 1, 70);
insert into shc values(2, 2, 60);
insert into shc values(2, 3, 80);
insert into shc values(3, 1, 80);
insert into shc values(3, 2, 80);
insert into shc values(3, 3, 80);
insert into shc values(4, 1, 50);
insert into shc values(4, 2, 30);
insert into shc values(4, 3, 20);
insert into shc values(5, 1, 76);
insert into shc values(5, 2, 87);
insert into shc values(6, 1, 31);
insert into shc values(6, 3, 34);
insert into shc values(7, 2, 89);
insert into shc values(7, 3, 98);

 

Guess you like

Origin blog.csdn.net/debimeng/article/details/104253446