select簡單循環嵌套


          訪問學生的物理最高成績,並且打印出來,單個要打印出所有的信息 在添加幾個 and 就可以啦。

select  student.gender,student.sname from student where

student.sid =( select sid from s_mark where s_mark.score= (select max(score) from s_mark   where s_mark.cid =(select cid from course where cname='physics' ))  

and    s_mark.cid = (select cid from course where cname='physics'));

步驟:

      1、先通過course表得到物理的cid

                  select cid from course where cname='physics'

       2、用得到的cid確定s_mark表中的最高成績

                      s_mark.score  =  (   select     max(score)    from s_mark     where s_mark.cid =(select cid from course where cname='physics' ) 

          3、通過最高成績和cid確定唯一的sid 

                                  上面這個一長串

                       
-----+-----+-----+-------+
| mid | sid | cid | score |  //表;s_mark
+-----+-----+-----+-------+
|   1 |   1 |   1 |    80 |
|   2 |   2 |   1 |    85 |
|   3 |   3 |   1 |    90 |
|   4 |   1 |   2 |    60 |
|   5 |   2 |   2 |    90 |
|   6 |   3 |   3 |    75 |
|   7 |   1 |   3 |    95 |
|   8 |   2 |   3 |    75 |
|   9 |   3 |   3 |    85 |
+-----+-----+-----+-------+
-----+-------+--------+
| sid | sname | gender | //表;student
+-----+-------+--------+
|   1 | Tom   | male   |
|   2 | Jack  | male   |
|   3 | Rose  | female |
+-----+-------+--------+
+-----+-----------+
| cid | cname     |//表;course
+-----+-----------+
|   1 | math      |
|   2 | physics   |
|   3 | chemistry |
+-----+-----------+

總結:都是圍繞着最基本的格式,

select * from course,stduent,s_mark  where(…….)  and (………);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM