MySQL database practice -1

Search "02" high course grade student information and course grade "01" than the course


 

Analysis: The title can be seen related to the course grade, student information, so only students from the Student table and SC results table to start solving the problem lies in the difficulty, including 01,02,03 courses on student achievement tables. results, how in comparison with a table top? of course, is to split them out, we can put 01 middle school curriculum information SC table all out and put a new table t1, t1 there SId fields and new fields class1, here class1 is achieving students 01 courses; then keep step as extract 02 course information SC in the formation of a new table t2, t2 in the field is a man after SId and class2 select two new table 01 course grade greater than 02 course grade, and finally connected. Student table to the right.

Student Table

--------------------- + -------- + ------ + + ------ + 
| the SId | Sname | Sage | SSEX | 
+ ------ + ------ + -------- + --------------------- + 
| 01    | Chao Lei | 1990 - 01 - 01  00 : 00 : 00 | M | 
| 02    | money power | 1990 - 12 - 21  00 : 00 : 00 | M | 
| 03    | Sun wind | 1990 - 12 - 20  00 : 00 : 00 | M | 
|04    | Li Yun | 1990 - 12 - 06  00 : 00 : 00 | M | 
| 05    | Zhou Mei | 1991 - 12 - 01  00 : 00 : 00 | F | 
| 06    | Wu Lan | 1992 - 01 - 01  00 : 00 : 00 | F | 
| 07    | Zheng bamboo | 1989 - 01 - 01  00 : 00: 00 | F | 
| 09    | Joe Smith | 2017 - 12 - 20  00 : 00 : 00 | F | 
| 10    | John Doe | 2017 - 12 - 25  00 : 00 : 00 | F | 
| 11    | John Doe | 2012 - 06 - 06  00 : 00 : 00 | F | 
| 12    | Zhao six | 2013 - 06 - 13 00 : 00 : 00 | F | 
| 13    | SUN seven | 2014 - 06 - 01  00 : 00 : 00 | F | 
+ ------ + -------- + ------ --------------- + ------ +

sc table

+------+------+-------+
| SId  | CId  | score |
+------+------+-------+
| 01   | 01   |  80.0 |
| 01   | 02   |  90.0 |
| 01   | 03   |  99.0 |
| 02   | 01   |  70.0 |
| 02   | 02   |  60.0 |
| 02   | 03   |  80.0 |
| 03   | 01   |  80.0 |
| 03   | 02   |  80.0 |
| 03   | 03   |  80.0 |
| 04   | 01   |  50.0 |
| 04   | 02   |  30.0 |
| 04   | 03   |  20.0 |
| 05   | 01   |  76.0 |
| 05   | 02   |  87.0 |
| 06   | 01   |  31.0 |
| 06   | 03   |  34.0 |
| 07   | 02   |  89.0 |
| 07   | 03   |  98.0 |
+------+------+-------+

Construction of the new table t1

select SId,score as class1 from sc where sc.CId='01';

t1 query results:

+------+--------+
| SId  | class1 |
+------+--------+
| 01   |   80.0 |
| 02   |   70.0 |
| 03   |   80.0 |
| 04   |   50.0 |
| 05   |   76.0 |
| 06   |   31.0 |
+------+--------+

Construction of the new table t2

select SId,score as class2 from sc where sc.CId='02';

t2 results:

+------+--------+
| SId  | class2 |
+------+--------+
| 01   |   90.0 |
| 02   |   60.0 |
| 03   |   80.0 |
| 04   |   30.0 |
| 05   |   87.0 |
| 07   |   89.0 |
+------+--------+

Screening of 01 students course grade greater than 02 course grade

select t1.SId, class1, class2 from
          (select SId, score as class1 from sc where sc.CId = '01')as t1, 
          (select SId, score as class2 from sc where sc.CId = '02')as t2
where t1.SId = t2.SId AND t1.class1 > t2.class2;

search result:

+------+--------+--------+
| SId  | class1 | class2 |
+------+--------+--------+
| 02   |   70.0 |   60.0 |
| 04   |   50.0 |   30.0 |
+------+--------+--------+

Then it is simple, direct access to student information Student table 02, 04 students, then class1 and class2 ground behind the line, we can step on the results of the query table named r

select * from Student RIGHT JOIN (
    select t1.SId, class1, class2 from
          (select SId, score as class1 from sc where sc.CId = '01')as t1, 
          (select SId, score as class2 from sc where sc.CId = '02')as t2
    where t1.SId = t2.SId AND t1.class1 > t2.class2
)r 
on Student.SId = r.SId;

Final Results:

+ -------- + ------- + ------ + ------ + ---- - + -------- + -------- + 
| the SId | Sname | Sage | SSEX | the SId | class1 | Class2 | 
+ ------ + ------ - + --------------------- + ------ + ------ + -------- + - + ------ 
| 02    | money power | 1990 - 12 - 21  00 : 00 : 00 | M | 02    |    70.0 |    60.0 | 
| 04    | Li Yun | 1990 - 12 - 06  00 : 00 : 00 | Male | 04    |    50.0 |   30.0 |
+------+--------+---------------------+------+------+--------+--------+

the above

Guess you like

Origin www.cnblogs.com/lattesea/p/12159301.html