insert into student values('108','曾華','男','1977-09-01','95033');
insert into student values('105','匡明','男','1975-10-02','95031');
insert into student values('107','王麗','女','1976-01-23','95033');
insert into student values('101','李軍','男','1976-02-20','95033');
insert into student values('109','王芳','女','1975-02-10','95031');
insert into student values('103','陸君','男','1974-06-03','95031');
mysql> desc student;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| sno | varchar(20) | NO | PRI | NULL | |
| sname | varchar(20) | NO | | NULL | |
| ssex | varchar(10) | NO | | NULL | |
| sbirthday | datetime | YES | | NULL | |
| class | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
向教師表添加數據
insert into teacher values('804','李成','男','1958-12-02','副教授','計算機系');
insert into teacher values('856','張旭','男','1969-03-12','講師','電子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','計算機系');
insert into teacher values('831','劉冰','女','1977-08-14','助教','電子工程系');
insert into course values('3-105','計算機導論','825');
insert into course values('3-245','操作系統','804');
insert into course values('6-166','數字電路','856');
insert into course values('9-888','高等數學','831');
mysql> desc course;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| cno | varchar(20) | NO | PRI | NULL | |
| cname | varchar(20) | NO | | NULL | |
| tno | varchar(20) | NO | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
向成績表添加數據
insert into score values('103','3-245','86');
insert into score values('105','3-245','75');
insert into score values('109','3-245','68');
insert into score values('103','3-105','92');
insert into score values('105','3-105','88');
insert into score values('109','3-105','76');
insert into score values('103','6-166','85');
insert into score values('105','6-166','79');
insert into score values('109','6-166','81');
/*查詢studnet的所有記錄*/
select * from student;
/*查詢部分記錄*/
select sname,ssex,class from student;
/*查詢教師所有但聞即不重複的depart列*/
select DISTINCT depart from teacher;
/*查詢score表中成績在60到80之間的*/
select * from score where degree BETWEEN 60 and 80;
select * from score where degree>60 and degree<80;
/*查詢score表中成績爲85,86或88的成績*/
select * from score where degree in(85,86,88);
/*查詢student表中95031班或性別爲女的同學記錄*/
select * from student where class='95031' or ssex='女';
/*以class降序查詢student表中所有記錄*/
select * from student order by class DESC;
/*以cno升序,degree降序查詢score表中的所有記錄*/
select * from score order by cno asc,degree desc;
/*查詢95031班的學生人數*/
select count(*) as num from student where class='95031';
/*查詢score表中的最高分的學生好和課程號(子查詢或者排序)排序可能有兩條符合條件的結果但只能返回一條*/
子查詢:select sno,cno from score where degree=(select max(degree) from score);
排序:select sno,cno,degree from score order by degree desc limit 0,1;
mysql> select avg(degree) from score where cno='3-105';
+-------------+
| avg(degree) |
+-------------+
| 85.33333333 |
+-------------+
1 row in set
mysql> select avg(degree) from score where cno='3-345';
+-------------+
| avg(degree) |
+-------------+
| NULL |
+-------------+
1 row in set
一句sql完成兩個操作 分組查詢 (爲cno分組)
mysql> select cno,avg(degree) from score group by cno;
+-------+-------------+
| cno | avg(degree) |
+-------+-------------+
| 3-105 | 85.33333333 |
| 3-245 | 76.33333333 |
| 6-166 | 81.66666667 |
+-------+-------------+
3 rows in set
查詢score表中至少有兩名學生選修的並以3開頭的課程的平均成績
查詢哪些課程是至少兩名以上學生選修的
mysql> select cno from score group by cno having count(cno)>=2;
+-------+
| cno |
+-------+
| 3-105 |
| 3-245 |
| 6-166 |
+-------+
3 rows in set
查詢哪些課程是至少兩名以上學生選修的並且是3開頭的
mysql> select cno from score group by cno having count(cno)>=2 and cno like '3%';
+-------+
| cno |
+-------+
| 3-105 |
| 3-245 |
+-------+
2 rows in set
查詢滿足條件的平均成績
mysql> select cno ,avg(degree)from score group by cno having count(cno)>=2 and cno like '3%';
+-------+-------------+
| cno | avg(degree) |
+-------+-------------+
| 3-105 | 85.33333333 |
| 3-245 | 76.33333333 |
+-------+-------------+
2 rows in set
查詢分數大於70小於90的sno列
mysql> select sno,degree from score where degree between 70 and 90;
mysql> select sno,degree from score where degree<90 and degree>70;
+-----+--------+
| sno | degree |
+-----+--------+
| 103 | 86 |
| 103 | 85 |
| 105 | 88 |
| 105 | 75 |
| 105 | 79 |
| 109 | 76 |
| 109 | 81 |
+-----+--------+
7 rows in set
mysql> select cno,avg(degree) from score where sno in (select sno from student where class='95031') group by cno;
+-------+-------------+
| cno | avg(degree) |
+-------+-------------+
| 3-105 | 85.33333333 |
| 3-245 | 76.33333333 |
| 6-166 | 81.66666667 |
+-------+-------------+
3 rows in set
查詢95031班學生每門課的名稱和平均成績
mysql> select s.cno,c.cname,avg(degree) from score s,course c where s.sno in(select sno from student where class='95031') and s.cno=c.cno group by s.cno;
+-------+------------+-------------+
| cno | cname | avg(degree) |
+-------+------------+-------------+
| 3-105 | 計算機導論 | 85.33333333 |
| 3-245 | 操作系統 | 76.33333333 |
| 6-166 | 數字電路 | 81.66666667 |
+-------+------------+-------------+
3 rows in set
查詢選修3-105課程的成績高於109號同學3-105課程成績的所有同學記錄
查詢109號同學3-105課程的成績
mysql> select degree from score where sno='109' and cno='3-105';
+--------+
| degree |
+--------+
| 76 |
+--------+
1 row in set
查詢所有選修3-105課程的成績高於109號同學3-105課程成績的學生
mysql> select * from score where degree>(select degree from score where sno='109' and cno='3-105') and cno='3-105';
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 105 | 3-105 | 88 |
+-----+-------+--------+
2 rows in set