學生表studentmysql
學號:姓名:性別:出生日期:所在班級;sql
create table student ( sno varchar(20) primary key, sname varchar(20) not null, ssex varchar(10) not null, sbirthday datetime, class varchar(10) );
教師表teacher操作系統
教師編號,教師名字,教師性別,出生日期,職稱,所在部門。code
create table teacher( tno varchar(20) primary key, tname varchar(20) not null, tsex varchar(10) not null, tbirthday datetime, prof varchar(20), depart varchar(20) not null );
課程表course排序
課程號,課程名稱,教師編號ci
create table course( cno varchar(20) primary key, cname varchar(20) not null, tno varchar(20) not null, foreign key(tno) references teacher(tno) );
成績表score數學
學號,課程號,成績it
create table score( sno varchar(20) not null, cno varchar(20) not null, degree decimal, foreign key(sno) references student(sno), foreign key(cno) references course(cno), primary key(sno,cno) );
添加數據:table
insert into student values('101', '曾華','男','1977-09-01','95033'); insert into student values('102','匡明','男','1975-10-02', '95031'); insert into student values('103','王麗','女','1976-01-23', '95033'); insert into student values('104', '李軍','男','1976-02-20','95033'); insert into student values('105','王芳','女','1975-02-10', '95031'); insert into student values('106','陸君','男','1974-06-03','95031'); insert into student values('107','王尼瑪','男','1974-06-03','95031'); insert into student values('108','張全蛋','男','1974-06-03','95031'); insert into student values('109','趙鐵柱','男','1974-06-03','95031'); 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'); insert into score values('103','3-105','92'); insert into score values('103','3-245','86'); insert into score values('103','6-166','85'); insert into score values('105','3-105','88'); insert into score values('105','3-245','75'); insert into score values('105','6-166','79'); insert into score values('109','3-105','76'); insert into score values('109','3-245','68'); insert into score values('109','6-166','81');
mysql> select * from student; +-----+-----------+------+---------------------+-------+ | sno | sname | ssex | sbirthday | class | +-----+-----------+------+---------------------+-------+ | 101 | 曾華 | 男 | 1977-09-01 00:00:00 | 95033 | | 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 | | 103 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 | | 104 | 李軍 | 男 | 1976-02-20 00:00:00 | 95033 | | 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 | | 106 | 陸君 | 男 | 1974-06-03 00:00:00 | 95031 | | 107 | 王尼瑪 | 男 | 1974-06-03 00:00:00 | 95031 | | 108 | 張全蛋 | 男 | 1974-06-03 00:00:00 | 95031 | | 109 | 趙鐵柱 | 男 | 1974-06-03 00:00:00 | 95031 | +-----+-----------+------+---------------------+-------+ 9 rows in set (0.00 sec)
mysql> select sname, ssex, class from student; +-----------+------+-------+ | sname | ssex | class | +-----------+------+-------+ | 曾華 | 男 | 95033 | | 匡明 | 男 | 95031 | | 王麗 | 女 | 95033 | | 李軍 | 男 | 95033 | | 王芳 | 女 | 95031 | | 陸君 | 男 | 95031 | | 王尼瑪 | 男 | 95031 | | 張全蛋 | 男 | 95031 | | 趙鐵柱 | 男 | 95031 | +-----------+------+-------+ 9 rows in set (0.00 sec)
關鍵字distinct
排除重複。class
mysql> select distinct depart from teacher; +-----------------+ | depart | +-----------------+ | 計算機系 | | 電子工程系 | +-----------------+ 2 rows in set (0.01 sec)
查詢區間:
方式一:between...and...
mysql> select * from score where degree between 60 and 80; +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 105 | 3-245 | 75 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 109 | 3-245 | 68 | +-----+-------+--------+ 4 rows in set (0.00 sec)
方式二:直接使用運算符比較;
mysql> select * from score where degree > 60 and degree < 80; +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 105 | 3-245 | 75 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 109 | 3-245 | 68 | +-----+-------+--------+ 4 rows in set (0.00 sec)
表示或着關係的查詢;in關鍵字。
mysql> select * from score where degree in(85,86,88); +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 103 | 3-245 | 86 | | 103 | 6-166 | 85 | | 105 | 3-105 | 88 | +-----+-------+--------+ 3 rows in set (0.00 sec)
關鍵字or
,能夠是不一樣的字段
mysql> select * from student where class='95031' or ssex='女'; +-----+-----------+------+---------------------+-------+ | sno | sname | ssex | sbirthday | class | +-----+-----------+------+---------------------+-------+ | 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 | | 103 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 | | 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 | | 106 | 陸君 | 男 | 1974-06-03 00:00:00 | 95031 | | 107 | 王尼瑪 | 男 | 1974-06-03 00:00:00 | 95031 | | 108 | 張全蛋 | 男 | 1974-06-03 00:00:00 | 95031 | | 109 | 趙鐵柱 | 男 | 1974-06-03 00:00:00 | 95031 | +-----+-----------+------+---------------------+-------+ 7 rows in set (0.00 sec)
排序的關鍵字:order by...
降序:desc
升序:asc
默承認以不寫
mysql> select * from student order by class desc; +-----+-----------+------+---------------------+-------+ | sno | sname | ssex | sbirthday | class | +-----+-----------+------+---------------------+-------+ | 101 | 曾華 | 男 | 1977-09-01 00:00:00 | 95033 | | 103 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 | | 104 | 李軍 | 男 | 1976-02-20 00:00:00 | 95033 | | 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 | | 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 | | 106 | 陸君 | 男 | 1974-06-03 00:00:00 | 95031 | | 107 | 王尼瑪 | 男 | 1974-06-03 00:00:00 | 95031 | | 108 | 張全蛋 | 男 | 1974-06-03 00:00:00 | 95031 | | 109 | 趙鐵柱 | 男 | 1974-06-03 00:00:00 | 95031 | +-----+-----------+------+---------------------+-------+ 9 rows in set (0.00 sec)
以逗號隔開。
mysql> select * from score order by cno asc,degree desc; +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 103 | 3-105 | 92 | | 105 | 3-105 | 88 | | 109 | 3-105 | 76 | | 103 | 3-245 | 86 | | 105 | 3-245 | 75 | | 109 | 3-245 | 68 | | 103 | 6-166 | 85 | | 109 | 6-166 | 81 | | 105 | 6-166 | 79 | +-----+-------+--------+ 9 rows in set (0.00 sec)
統計:count
mysql> select * from student; +-----+-----------+------+---------------------+-------+ | sno | sname | ssex | sbirthday | class | +-----+-----------+------+---------------------+-------+ | 101 | 曾華 | 男 | 1977-09-01 00:00:00 | 95033 | | 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 | | 103 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 | | 104 | 李軍 | 男 | 1976-02-20 00:00:00 | 95033 | | 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 | | 106 | 陸君 | 男 | 1974-06-03 00:00:00 | 95031 | | 107 | 王尼瑪 | 男 | 1974-06-03 00:00:00 | 95031 | | 108 | 張全蛋 | 男 | 1974-06-03 00:00:00 | 95031 | | 109 | 趙鐵柱 | 男 | 1974-06-03 00:00:00 | 95031 | +-----+-----------+------+---------------------+-------+ 9 rows in set (0.00 sec)
子查詢或者排序;
複合語句。
mysql> select sno,cno from score where degree=(select max(degree) from score); +-----+-------+ | sno | cno | +-----+-------+ | 103 | 3-105 | +-----+-------+ 1 row in set (0.00 sec)
步驟:
①、找到最高分
select max(degree) from score;
②、找最高分的sno和cno
mysql> select sno,cno from score where degree=(select max(degree) from score);
③、排序的作法:
存在必定缺陷,好比成績最高有多個分相同。
mysql> select sno,cno,degree from score order by degree desc; +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 103 | 3-105 | 92 | | 105 | 3-105 | 88 | | 103 | 3-245 | 86 | | 103 | 6-166 | 85 | | 109 | 6-166 | 81 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 105 | 3-245 | 75 | | 109 | 3-245 | 68 | +-----+-------+--------+ 9 rows in set (0.00 sec)
limit
第一個數字表示從第幾條開始,第二個數字表示查多少條。
mysql> select sno,cno,degree from score order by degree desc limit 0,1; +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 103 | 3-105 | 92 | +-----+-------+--------+ 1 row in set (0.00 sec)
avg()
查詢一門的平均成績;
mysql> select avg(degree) from score where cno='3-105'; +-------------+ | avg(degree) | +-------------+ | 85.3333 | +-------------+ 1 row in set (0.00 sec)
一個語句中查詢多門課程的平均成績;...group by...
分組
mysql> select cno,avg(degree) from score group by cno; +-------+-------------+ | cno | avg(degree) | +-------+-------------+ | 3-105 | 85.3333 | | 3-245 | 76.3333 | | 6-166 | 81.6667 | +-------+-------------+ 3 rows in set (0.00 sec)
分組條件與模糊查詢
mysql> select cno,avg(degree),count(*) from score group by cno having count(cno)>=2 and cno like '3%'; +-------+-------------+----------+ | cno | avg(degree) | count(*) | +-------+-------------+----------+ | 3-105 | 85.3333 | 3 | | 3-245 | 76.3333 | 3 | +-------+-------------+----------+ 2 rows in set (0.00 sec)
分組條件:group by...having...
模糊查詢:like
範圍查詢的兩種方式
方式一:
mysql> select sno,degree from score where degree>70 and degree<90; +-----+--------+ | sno | degree | +-----+--------+ | 103 | 86 | | 103 | 85 | | 105 | 88 | | 105 | 75 | | 105 | 79 | | 109 | 76 | | 109 | 81 | +-----+--------+ 7 rows in set (0.00 s
方式二:between...and...
mysql> select sno,degree from score where degree between 70 and 90; +-----+--------+ | sno | degree | +-----+--------+ | 103 | 86 | | 103 | 85 | | 105 | 88 | | 105 | 75 | | 105 | 79 | | 109 | 76 | | 109 | 81 | +-----+--------+ 7 rows in set (0.00 sec)
多表查詢
mysql> select sname,cno,degree from student,score where student.sno=score.sno; +-----------+-------+--------+ | sname | cno | degree | +-----------+-------+--------+ | 王麗 | 3-105 | 92 | | 王麗 | 3-245 | 86 | | 王麗 | 6-166 | 85 | | 王芳 | 3-105 | 88 | | 王芳 | 3-245 | 75 | | 王芳 | 6-166 | 79 | | 趙鐵柱 | 3-105 | 76 | | 趙鐵柱 | 3-245 | 68 | | 趙鐵柱 | 6-166 | 81 | +-----------+-------+--------+ 9 rows in set (0.00 sec)
mysql> select sno,cname,degree from course,score where course.cno=score.cno; +-----+-----------------+--------+ | sno | cname | degree | +-----+-----------------+--------+ | 103 | 計算機導論 | 92 | | 103 | 操做系統 | 86 | | 103 | 數字電路 | 85 | | 105 | 計算機導論 | 88 | | 105 | 操做系統 | 75 | | 105 | 數字電路 | 79 | | 109 | 計算機導論 | 76 | | 109 | 操做系統 | 68 | | 109 | 數字電路 | 81 | +-----+-----------------+--------+ 9 rows in set (0.05 sec)
三表關聯查詢,經過共同字段的相等關係來聯繫在一塊兒。
mysql> select sname,cname,degree from student,course,score where student.sno=score.sno and course.cno = score.cno; +-----------+-----------------+--------+ | sname | cname | degree | +-----------+-----------------+--------+ | 王麗 | 計算機導論 | 92 | | 王麗 | 操做系統 | 86 | | 王麗 | 數字電路 | 85 | | 王芳 | 計算機導論 | 88 | | 王芳 | 操做系統 | 75 | | 王芳 | 數字電路 | 79 | | 趙鐵柱 | 計算機導論 | 76 | | 趙鐵柱 | 操做系統 | 68 | | 趙鐵柱 | 數字電路 | 81 | +-----------+-----------------+--------+ 9 rows in set (0.00 sec)
子查詢加分組求平均分
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 | 82.0000 | | 3-245 | 71.5000 | | 6-166 | 80.0000 | +-------+-------------+ 3 rows in set (0.08 sec)
子查詢
步驟: ① select degree from score where sno='109'and cno ='3-105'; ② select * from score where cno = '3-105' and degree > (select degree from score where sno='109'and cno ='3-105'); mysql> select * from score where cno = '3-105'and degree >(select degree from score where sno='109'and cno ='3-105'); +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 103 | 3-105 | 92 | | 105 | 3-105 | 88 | +-----+-------+--------+ 2 rows in set (0.00 sec)
mysql> SELECT * from score where degree > (SELECT degree FROM score where sno = '109' and cno = '3-105' ); +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 103 | 3-105 | 92 | | 103 | 3-245 | 86 | | 103 | 6-166 | 85 | | 105 | 3-105 | 88 | | 105 | 6-166 | 79 | | 109 | 6-166 | 81 | +-----+-------+--------+ 6 rows in set (0.00 sec)
year()+in
關鍵字的子查詢
mysql> SELECT sno,sname, sbirthday from student -> where YEAR(sbirthday) in (SELECT year(sbirthday) from student where sno in( 101,108)); +-----+-----------+---------------------+ | sno | sname | sbirthday | +-----+-----------+---------------------+ | 101 | 曾華 | 1977-09-01 00:00:00 | | 106 | 陸君 | 1974-06-03 00:00:00 | | 107 | 王尼瑪 | 1974-06-03 00:00:00 | | 108 | 張全蛋 | 1974-06-03 00:00:00 | | 109 | 趙鐵柱 | 1974-06-03 00:00:00 | +-----+-----------+---------------------+ 5 rows in set (0.00 sec)
多層嵌套子查詢
步驟: ① SELECT tno FROM teacher WHERE tname='張旭'; ② SELECT cno from course where tno =(SELECT tno FROM teacher WHERE tname='張旭'); ③ SELECT * FROM score WHERE cno =(SELECT cno from course where tno =(SELECT tno FROM teacher WHERE tname='張旭')); mysql> SELECT * FROM score WHERE cno =(SELECT cno from course where tno =(SELECT tno FROM teacher WHERE tname='張旭')); +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 103 | 6-166 | 85 | | 105 | 6-166 | 79 | | 109 | 6-166 | 81 | +-----+-------+--------+ 3 rows in set (0.00 sec)
多表查詢+子查詢
步驟: ① SELECT cno from score GROUP BY cno HAVING count(*)>5; ② select tno from course where cno=(SELECT cno from score GROUP BY cno HAVING count(*)>5); ③ SELECT tname from teacher WHERE tno = (select tno from course where cno=(SELECT cno from score GROUP BY cno HAVING count(*)>5)); mysql> SELECT tname from teacher WHERE tno = (select tno from course where cno=(SELECT cno from score GROUP BY cno HAVING count(*)>5)); +--------+ | tname | +--------+ | 王萍 | +--------+ 1 row in set (0.00 sec)
in表示或者關係
SELECT * from student WHERE class IN(95033,95031);
select cno from score where degree>85; mysql> SELECT cno,degree FROM score WHERE degree>85; +-------+--------+ | cno | degree | +-------+--------+ | 3-105 | 90 | | 3-105 | 91 | | 3-105 | 92 | | 3-245 | 86 | | 3-105 | 88 | | 3-105 | 88 | +-------+--------+ 6 rows in set (0.00 sec)