<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    人在江湖

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

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

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


    數據可以在這里下載,只是用作練習,沒做任何外鍵關聯: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. 查詢所有同學的學號、姓名、選課數、總成績;
    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 人在江湖 閱讀(2122) 評論(0)  編輯  收藏 所屬分類: others
    主站蜘蛛池模板: 免费播放春色aⅴ视频| 欧美在线看片A免费观看| 亚洲AV中文无码乱人伦| 亚洲欧美日韩国产成人| 国产精品无码免费播放| 亚洲色偷精品一区二区三区| 一二三四免费观看在线电影 | 国产精品免费大片| 久久精品国产精品亚洲蜜月| 国产精品99精品久久免费| 亚洲国产人成网站在线电影动漫| 国产成人AV片无码免费| 久久亚洲国产成人精品性色| 114级毛片免费观看| 77777午夜亚洲| 免费观看国产小粉嫩喷水| 日韩在线视频免费| 国产精品亚洲片在线观看不卡| 免费无码又爽又刺激高潮软件| 久久久久亚洲AV成人无码| 亚洲精品在线免费看| 亚洲人成色在线观看| 免费va在线观看| 国产在线国偷精品免费看| 亚洲精品乱码久久久久66| 日韩免费观看一区| 亚洲一级高清在线中文字幕| 日本一区免费电影| 91精品成人免费国产| 亚洲成人黄色在线| 在线观看免费亚洲| 黄色视屏在线免费播放| 亚洲精品第五页中文字幕| 国产精品极品美女免费观看| 两性色午夜免费视频| 亚洲国产成人久久| 亚洲第一页日韩专区| 久久A级毛片免费观看| 欧美日韩亚洲精品| 亚洲AV无码久久精品狠狠爱浪潮| 久久精品a一国产成人免费网站|