mysql-sql查詢

學生表

create table student(

    sno varchar(20) primary key,

    sname varchar(20) not null,

    ssex varchar(10) not null,

    sbirthday datetime,

    class varchar(20)

);

課程表

create table course(

    cno varchar(20) primary key,

    cname varchar(20) not null,

    tno varchar(20) not null,

    foreign key (tno) refrences teacher(tno)

);

教師表

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

);

成績表

create table sorce(

    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)

);

向學生表添加數據

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','助教','電子工程系');

mysql> desc teacher;

+-----------+-------------+------+-----+---------+-------+

| Field    | Type        | Null | Key | Default | Extra |

+-----------+-------------+------+-----+---------+-------+

| tno      | varchar(20) | NO  | PRI | NULL    |      |

| tname    | varchar(20) | NO  |    | NULL    |      |

| tsex      | varchar(10) | NO  |    | NULL    |      |

| tbirthday | datetime    | YES  |    | NULL    |      |

| prof      | varchar(20) | YES  |    | NULL    |      |

| depart    | varchar(20) | NO  |    | NULL    |      |

+-----------+-------------+------+-----+---------+-------+

向課程表添加數據

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');

mysql> select * from score;

+-----+-------+--------+

| sno | cno  | degree |

+-----+-------+--------+

| 103 | 3-245 | 86    |

| 109 | 3-245 | 68    |

+-----+-------+--------+

查詢

/*查詢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 * from course;

+-------+------------+-----+

| cno  | cname      | tno |

+-------+------------+-----+

| 3-105 | 計算機導論 | 825 |

| 3-245 | 操作系統  | 804 |

| 6-166 | 數字電路  | 856 |

| 9-888 | 高等數學  | 831 |

+-------+------------+-----+

4 rows in set

查詢課程平均成績

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

查詢所有學生的sname、cno和degree列

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

查詢所有學生的sname、cname和degree列

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

查詢所有學生的sname,cname和degree列(查詢所有學生的所有課程的成績)

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

查詢95031班學生每門課的平均分

查詢哪些學生是95031班的

mysql> select * from student where class='95031';

+-----+--------+------+---------------------+-------+

| sno | sname  | ssex | sbirthday          | class |

+-----+--------+------+---------------------+-------+

| 103 | 陸君  | 男  | 1974-06-03 00:00:00 | 95031 |

| 105 | 匡明  | 男  | 1975-10-02 00:00:00 | 95031 |

| 109 | 王芳  | 女  | 1975-02-10 00:00:00 | 95031 |

| 110 | 石恆恆 | 男  | 1998-03-13 00:00:00 | 95031 |

+-----+--------+------+---------------------+-------+

4 rows in set

查詢95031班的學生的全部成績

mysql> select * from score where sno in (select sno from student where class='95031');

+-----+-------+--------+

| sno | cno  | degree |

+-----+-------+--------+

| 103 | 3-105 | 92    |

| 103 | 3-245 | 86    |

| 103 | 6-166 | 85    |

| 105 | 3-105 | 88    |

| 105 | 3-245 | 75    |

| 105 | 6-166 | 79    |

| 109 | 3-105 | 76    |

| 109 | 3-245 | 68    |

| 109 | 6-166 | 81    |

+-----+-------+--------+

9 rows in set

查詢95031班學生的每門課的平均成績

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章