Select 语句语法 select *from xx left join xx on
[where 条件字句]
[group by 字句]
[having 字句]
[order by字句]
[limit 字句]
create table student(
Sno VARCHAR(20) not null PRIMARY key ,
Sname varchar(20) not null,
Ssex varchar(20) not null,
Sbirthday DATetime,
-- Class varchar(20) );
create table TEACHER(
Tno VARCHAR(20) not null PRIMARY key ,
Tname varchar(20) not null,
Tsex varchar(20) not null,
Tbirthday DATetime,
prof varchar(20) ,
depart varchar(20) not null);
create table course(
Cno VARCHAR(20) not null PRIMARY key ,
Cname varchar(20) not null,
Tno varchar(20) not null,
CONSTRAINT stu_tea_fk foreign key (Tno) REFERENCES teacher(Tno));
create table score(
Sno VARCHAR(20) not null ,
Cno varchar(20) not null,
Degree decimal(4,1),
primary key(Sno,Cno),
CONSTRAINT cou_SCO_fk foreign key (cno) REFERENCES course(cno),
CONSTRAINT stu_SCO_fk foreign key (Sno) REFERENCES STUDENT(Sno));
INSERT into student VALUES('108','曾华','男','1977-09-01','95033' ),
('105','匡明','男','1975-10-02','95031' ),
('107','王丽','女','1976-01-23','95033' ),
('101','李军','男','1976-02-20','95033' ),
('109','王芳','女','1975-02-10','95031' ),
('103','陆君','男','1974-06-03','95031' );
INSERT into teacher VALUES
('804','李诚','男','1958-12-02','副教授','计算机系' ),
('856','张旭','男','1969-03-12' ,'讲师','电子工程系'),
('825','王萍','女','1972-05-05' ,'助教','计算机系'),
('831','刘冰','女','1977-08-14','助教','电子工程系');
INSERT into COURSE VALUES
('3-105','计算机导论','825'),
('3-245','操作系统','804'),
('6-166','数字电路','856'),
('9-888','高等数学','831');
INSERT into score VALUES
('103','3-245','86'),
('105','3-245','75'),
('109','3-245','68'),
('103','3-105','92'),
('105','3-105','88'),
('109','3-105','76'),
('101','3-105','64'),
('107','3-105','91'),
('108','3-105','78'),
('101','6-166','85'),
('107','6-166','79'),
('108','6-166','81');
select count(*) from student where class='95031';
select Sno,Cno,MAX(Degree) from score;
select Cno,count(Sno),avg(degree) from score where Cno like "3%" group by Cno having count(Sno)>5;
SELECT AVG(DEGREE),cno FROM SCORE GROUP BY cNO;
select st.sno,st.sname,sc.cno,sc.degree from student st left JOIN score sc ON
sc.Sno=st.Sno;
select st.class,avg(sc.degree ) from student st left JOIN score sc ON
sc.Sno=st.Sno where st.class='95033';
select te.tname,avg(sc.degree) from teacher te inner join course co inner join score sc ON
te.TNO=co.tno and co.cno=sc.cno WHERE te.tname='张旭';
select te.tname from teacher te inner join course co inner join score sc INNER JOIN student st ON
te.TNO=co.tno and co.cno=sc.cno and sc.sno=st.sno GROUP BY te.tname having count(te.tname)>5;
select te.depart,sc.* from teacher te inner join course co inner join score sc INNER JOIN student st ON
te.TNO=co.tno and co.cno=sc.cno and sc.sno=st.sno where te.depart='计算机系';
select st.sname,sc.* from teacher te inner join course co inner join score sc INNER JOIN student st ON
te.TNO=co.tno and co.cno=sc.cno and sc.sno=st.sno where
st.ssex='男' order by co.cname='计算机导论';
select st.sname ,te.depart,sc.* from teacher te inner join course co inner join score sc INNER JOIN student st ON
te.TNO=co.tno and co.cno=sc.cno and sc.sno=st.sno where st.sname in
(select st.sname from teacher te inner join course co inner join score sc INNER JOIN student st ON
te.TNO=co.tno and co.cno=sc.cno and sc.sno=st.sno where co.cname='计算机导论' and
st.ssex='男');
create VIEW v_ssss as select * from teacher te inner join course co inner join score sc INNER JOIN student st ON
te.TNO=co.tno and co.cno=sc.cno and sc.sno=st.sno;