Java蜘蛛人 歡迎大家

          歡迎大家 來到我的blog , 如果我身邊的朋友 有什么不懂可以直接來問我 我會細心的幫助你的. 如果網絡上的朋友有什么不懂的 可以加我Java蜘蛛人 QQ48187537
          posts - 54, comments - 192, trackbacks - 0, articles - 1

          教大家SQL 內連接

          Posted on 2007-08-22 23:17 Java蜘蛛人 --鄭成橋 閱讀(2044) 評論(0)  編輯  收藏
          今天就隨便的講講.  內連接...              假如 你有2張表.. 一個是學生表. 一個是成績表. 你要查一個人的表的成績.  .你就要用到內連接 把2個表連接起來...


          下面簡單的教大家用 sql  來個內連接.~

          按照這步奏做..
           
           1create database zcq  --建數據庫
           2
           3create table student  /*         建一個學生表..     */
           4(
           5sno char(5),
           6sname char(5),
           7ssex char(2),
           8sage int
           9)
          10/* 插入一些數據             */
          11
          12insert into student values ('001','小橋','',18)
          13insert into student values ('002','寶玉','',20)
          14insert into student values ('003','哈哈','',17)
          15insert into student values ('004','暗暗','',16)


           
          /* 再建一個成績表  */
          create table score
          (
          sno 
          char(5),
          cno 
          int,
          score 
          int 
          )

          insert into score values ('001','1',80)
          insert into score values ('002','2',90)
          insert into score values ('003','3',50)
          insert into score values ('004','4',80)
          insert into score values ('005','5',90)
          insert into score values ('006','6',100)


           1create table course
           2(
           3cno int,
           4chame char(5
           5)
           6
           7
           8
           9insert into course values (1,'c')
          10insert int o course values (2,'sql')
          11insert into course values (3,'java')
          12

          查詢他們最高分 最低分..
           1
           2--分數最高
           3select top 1 cno,avg(score)
           4from score
           5group by cno
           6--having avg(score)>0
           7order by cno 
           8
           9
          10--分數最低
          11select top 1 cno,avg(score)
          12from score
          13group by cno
          14--having avg(score)>0
          15order by cno desc


          --查詢一個叫寶玉的總分 和平均分
          select sname , sum(score) as 總分,avg(score) as 平均分 from student inner join score on(student.sno=score.sno)
          where sname like '%玉'
          group by sname




          --查詢寶玉的sql成績
          select sname,score,sage,chame from score inner join student on(student.sno=score.sno) inner join course on(score.cno=course.cno)
          where sname='寶玉' and chame='sql'
          --年齡在17-20歲之間的女生
          select *from score inner join student on(student.sno=score.sno)  inner join course on(score.cno=course.cno)
          where sage between 15 and 20  and ssex='' and chame='sql' 



                                                                                                                              ___作者:  鄭成橋

          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 调兵山市| 大邑县| 高陵县| 隆德县| 宝应县| 古丈县| 灵宝市| 南江县| 衡阳县| 饶平县| 峡江县| 贵溪市| 汉阴县| 乌鲁木齐县| 额敏县| 泗阳县| 鄯善县| 舒兰市| 河池市| 页游| 武平县| 梁河县| 涞水县| 梁山县| 云浮市| 吉隆县| 扶风县| 玉门市| 陈巴尔虎旗| 凯里市| 通江县| 宣汉县| 贺州市| 孝感市| 铜梁县| 靖安县| 澎湖县| 肇庆市| 太仓市| 白沙| 孟州市|