|
Posted on 2007-08-22 23:17 Java蜘蛛人 --鄭成橋 閱讀(2043) 評論(0) 編輯 收藏
今天就隨便的講講. 內連接... 假如 你有2張表.. 一個是學生表. 一個是成績表. 你要查一個人的表的成績. .你就要用到內連接 把2個表連接起來... 下面簡單的教大家用 sql 來個內連接.~ 按照這步奏做..
1 create database zcq --建數據庫 2 3 create table student /**//* 建一個學生表.. */ 4 ( 5 sno char(5), 6 sname char(5), 7 ssex char(2), 8 sage int 9 ) 10 /**//* 插入一些數據 */ 11 12 insert into student values ('001','小橋','男',18) 13 insert into student values ('002','寶玉','女',20) 14 insert into student values ('003','哈哈','男',17) 15 insert 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)
1 create table course 2 ( 3 cno int, 4 chame char(5) 5 ) 6 7 8 9 insert into course values (1,'c') 10 insert int o course values (2,'sql') 11 insert into course values (3,'java') 12
查詢他們最高分 最低分..
1 2 --分數最高 3 select top 1 cno,avg(score) 4 from score 5 group by cno 6 --having avg(score)>0 7 order by cno 8 9 10 --分數最低 11 select top 1 cno,avg(score) 12 from score 13 group by cno 14 --having avg(score)>0 15 order 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'


___作者: 鄭成橋
|