人在江湖

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

          原本sql寫得也不好,近幾年數(shù)據(jù)庫用得少,sql更是荒廢了,最近復習sql,網(wǎng)上例 子很多,但都只是提供sql例子,沒有配套的數(shù)據(jù)用來自己練習和調(diào)試。俺覺得自 己試著寫寫sql,調(diào)試調(diào)試還是有幫助的,讀人家sql例子好像讀懂了,自己寫就未 必思路正確,調(diào)試得通,寫得簡潔。

           
          跟著網(wǎng)上流行的學生選課表的例子復習了一下: http://www.cnblogs.com/buzaixian/archive/2009/09/21/1571365.html
          這篇文字在網(wǎng)上被轉載爛了,里面有些sql適合用在應用系統(tǒng)里,有些“報表”的感 覺更重些,主要是想復習前者。前20條大體還挺好,后30條明顯偏報表風格了,而 且后面選例良莠不齊,選了12個例子做練習,(其實很多語法,case, any/all, union之類的都沒包括),用mysql數(shù)據(jù)庫,并共享自己造出來的數(shù)據(jù)。關于這12條 sql, 修正了原文中有紕漏的地方。
          sql是基本技能,若能寫得好也挺精彩的,還在繼續(xù)練習。絕不提倡努力寫復雜sql 解決業(yè)務問題。應用系統(tǒng)里如果存在很復雜的sql,往往揭示了業(yè)務邏輯向下泄露 到sql層的問題,不利于維護和擴展,雖然這樣確實常能提高運行效率。具體情況 自行取舍。
          下面的例子都是比較通用的sql, 其實針對特定的數(shù)據(jù)庫,需要學的也挺多,比如 oracle db的decode函數(shù), rowid, rownum, connect by 雖然不通用,但是很實用。


          數(shù)據(jù)可以在這里下載,只是用作練習,沒做任何外鍵關聯(lián):http://115.com/file/an2svxbe#sql-study.sql


          整理的sql在下面:
          Student(S#,Sname,Sage,Ssex) 學生表
          Course(C#,Cname,T#) 課程表
          SC(S#,C#,score) 成績表
          Teacher(T#,Tname) 教師表
          1. 選出每門功課都及格的學號
          select distinct `s#` from sc where `s#` not in (select `s#` from sc where score <60)
          2. 查詢“1”課程比“2”課程成績高的所有學生的學號;
          SELECT c01.`s#` from (select `s#`, `score` from sc where `c#`=1) c01,
                                    (select `s#`, `score` from sc where `c#`=2) c02
                               where c01.`s#` = c02.`s#` and c01.score > c02.score
          3.  查詢平均成績大于60分的同學的學號和平均成績;
          select `s#`, avg(score) from sc group by `s#` having avg(score) > 60
          4. 查詢所有同學的學號、姓名、選課數(shù)、總成績;
          select student.`s#`, student.`Sname`, count(`c#`), sum(score) from student left outer join sc on student.`s#` = sc.`s#` group by student.`s#`, sc.`s#`
          5.查詢沒學過“葉平”老師課的同學的學號、姓名;
          select student.`s#`, student.`Sname` from student where student.`s#` not in (select distinct(sc.`s#`) from teacher, course, sc where Tname='葉平' and teacher.`t#` = course.`t#` and sc.`c#`= course.`c#` )
          6. 查詢學過“001”并且也學過編號“002”課程的同學的學號、姓名
          select student.`s#`, student.sname from student, sc where student.`s#` = sc.`s#` and sc.`c#` = 1 and exists (select * from sc sc_2 where sc_2.`c#`=2 and sc.`s#`=sc_2.`s#`)
          7. 查詢學過“葉平”老師所教的所有課的同學的學號、姓名 (巧妙)
          select `s#`, sname from student where `s#` in
                 (select `s#` from sc, teacher, course where tname='葉平' and teacher.`t#`=course.`t#` and course.`c#`= sc.`c#` group by `s#` having count(sc.`c#`)=
                         (select count(`c#`) from teacher, course where tname='葉 平' and teacher.`t#`=course.`t#`) )
          8. 查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名 (有代表性)
          select `s#`, sname from (select student.`s#`, student.sname, score, (select score from sc sc_2 where student.`s#`=sc_2.`s#` and sc_2.`c#`=2) score2 from student , sc where
                 sc.`s#`=student.`s#` and sc.`c#`=1) s_2 where score2 < score
          9.查詢沒有學全所有課的同學的學號、姓名
          select student.`S#`, Sname from student, sc where student.`s#` = sc.`s#` group by `s#`, sname having count(`c#`) < (select count(`c#`) from course)
          10. 查詢至少有一門課與學號為“002”的同學所學相同的同學的學號和姓名;
          select distinct(sc.`s#`), sname from student, sc where student.`s#`=sc.`s#` and `c#` in (select `c#` from sc where `s#`=002)
          11. 把“SC”表中“葉平”老師教的課的成績都更改為此課程的平均成績;
          update sc inner join
                 (select sc2.`c#`, avg(sc2.score) score from sc sc2, teacher, course where
                        sc2.`c#`=course.`c#` and tname='葉平' and teacher.`t#` = course.`t#` and course.`c#`=sc2.`c#` group by course.`c#`) sc3 on sc.`c#`=sc3.`c#` set sc.score=sc3.score
          12. 查詢2號的同學學習的課程他都學了的同學的學號;(注意理解:where語句的 第一個條件過濾掉不滿足c#的記錄,再group by,就比較清晰)
          select `S#` from SC where `C#` in (select `C#` from SC where `S#`=2)
              group by `S#` having count(*)=(select count(*) from SC where `S#`=2);

          posted on 2012-06-26 09:29 人在江湖 閱讀(2125) 評論(0)  編輯  收藏 所屬分類: others
          主站蜘蛛池模板: 利津县| 房产| 新野县| 涞源县| 柯坪县| 镇江市| 宁夏| 大港区| 龙川县| 西昌市| 江阴市| 永顺县| 湄潭县| 甘泉县| 和硕县| 普洱| 张北县| 耿马| 镇安县| 天气| 兰州市| 双桥区| 洪湖市| 罗山县| 方正县| 桃园县| 德阳市| 宿州市| 论坛| 竹溪县| 手游| 义马市| 河北区| 博罗县| 威宁| 古丈县| 余庆县| 静宁县| 卓资县| 锡林郭勒盟| 酒泉市|