MySQL基础(查)

Posted ksh1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL基础(查)相关的知识,希望对你有一定的参考价值。

#新建一个表
create database exercise;

#查询表的信息
SELECT * FROM student;
SELECT * FROM score;

#查询student表的第二条到第四条的记录
SELECT * FROM student2 LIMIT 1,3;

#从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
SELECT Id,NAME,Department FROM student;

#从student表中查询计算机系和英语系的学生的信息
SELECT * FROM student WHERE department IN (‘计算机系‘,‘英语系‘);

#从student表中查询年龄18~22岁的学生信息
SELECT Id,NAME,sex,2019-birth AS age,department,address FROM student WHERE 2019-birth BETWEEN 18 AND 30;

#从student表中查询每个院系有多少人
SELECT department, COUNT(id) FROM student GROUP BY department;

SELECT COUNT(*), department FROM student GROUP BY department;

#查询李四的考试科目(c_name)和考试成绩(grade)
SELECT C_name,Grade FROM score WHERE Stu_id=(SELECT id FROM student WHERE name= ‘李四‘ );

#用连接的方式查询所有学生的信息和考试信息
SELECT student.id,name,sex,birth,department,address,c_name,grade FROM student,score WHERE student.id=score.stu_id;

#计算每个学生的总成绩
SELECT student.id,name,SUM(grade) FROM student,score WHERE student.id=score.stu_id GROUP BY id;

#计算每个考试科目的平均成绩
SELECT c_name,AVG(grade) FROM score GROUP BY c_name;


#查询计算机成绩低于95的学生信息
SELECT * FROM student WHERE id IN (SELECT stu_id FROM score WHERE c_name="计算机" and grade<95);

#查询同时参加计算机和英语考试的学生的信息
SELECT * FROM student WHERE id =ANY( SELECT stu_id FROM score WHERE stu_id IN (SELECT stu_id FROM score WHERE c_name= ‘计算机‘) AND c_name= ‘英语‘ );

#将计算机考试成绩按从高到低进行排序
SELECT stu_id, grade FROM score WHERE c_name= ‘计算机‘ ORDER BY grade DESC;

#从student表和score表中查询出学生的学号,然后合并查询结果
SELECT id FROM student UNION SELECT stu_id FROM score;

#查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
SELECT student.id, name,sex,birth,department, address, c_name,grade FROM student, score WHERE (name LIKE ‘张%‘ OR name LIKE ‘王%‘)AND student.id=score.stu_id ;

#查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
SELECT student.id, name,sex,birth,department, address, c_name,grade FROM student, score WHERE address LIKE ‘湖南%‘ AND student.id=score.stu_id;


#练习题三
#查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
SELECT DISTINCT x.cno,x.sno,x.degree FROM score2 x,score2 y WHERE x.Cno = ‘3-105‘ AND x.Degree > y.Degree AND y.Sno=‘109‘ AND y.Cno=‘3-105‘;

SELECT Degree FROM score2 WHERE Sno=‘109‘ AND Cno=‘3-105‘;
SELECT * FROM score2 WHERE Cno=‘3-105‘ AND Degree >(SELECT Degree FROM score2 WHERE Sno=‘109‘ AND Cno=‘3-105‘);

#查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
SELECT x.cno,x.sno,x.degree FROM score2 x,score2 y WHERE x.Degree > y.Degree AND y.Sno=‘109‘ AND y.Cno=‘3-105‘;

SELECT cno,sno,degree FROM score2 WHERE Degree > (SELECT Degree FROM score2 WHERE Cno=‘3-105‘ AND Sno=‘109‘);

SELECT * FROM score2 WHERE Degree >(SELECT Degree FROM score2 WHERE Sno=‘109‘ AND Cno=‘3-105‘);
#查询和学号为108、101的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
SELECT * FROM student2 WHERE sno in (101,108);
SELECT sbirthday FROM student2 WHERE sno in (101,108);
SELECT DATE_FORMAT(sbirthday,"%Y") FROM student2 WHERE sno in (101,108);
SELECT sno,sname,sbirthday FROM student2 WHERE DATE_FORMAT(sbirthday,"%Y") in (SELECT DATE_FORMAT(sbirthday,"%Y") FROM student2 WHERE sno in (101,108));


#查询“张旭“教师任课的学生成绩。
SELECT * FROM teacher WHERE Tname="张旭";
SELECT tno FROM teacher WHERE Tname="张旭";
SELECT cno FROM course WHERE Tno in (SELECT tno FROM teacher WHERE Tname="张旭");
SELECT * FROM score2 WHERE Cno in (SELECT cno FROM course WHERE Tno in (SELECT tno FROM teacher WHERE Tname="张旭"));

#查询选修某课程的同学人数多于5人的教师姓名。没完成
SELECT COUNT(cno),cno FROM score2 GROUP BY Cno;
SELECT COUNT(cno),cno FROM score2 GROUP BY Cno HAVING COUNT(Cno)>5;
SELECT cno FROM score2 GROUP BY Cno HAVING COUNT(Cno)>5;

 

#查询95033班和95031班全体学生的记录。

 

#查询存在有85分以上成绩的课程Cno.
SELECT MAX(degree), cno FROM score2 GROUP BY Cno;
SELECT MAX(Degree), cno FROM score2 GROUP BY Cno HAVING MAX(Degree)>85;
SELECT cno FROM score2 GROUP BY Cno HAVING MAX(Degree)>85;
SELECT * FROM course WHERE Cno in(SELECT cno FROM score2 GROUP BY Cno HAVING MAX(Degree)>85);


#查询出“计算机系“教师所教课程的成绩表。

 


#查询“计算 机系”与“电子工程系“不同职称的教师的Tname和Prof。

 

#查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
SELECT degree FROM score2 WHERE Cno="3-105";

SELECT degree FROM score2 WHERE Cno="3-245";

SELECT * FROM score2 WHERE Cno="3-105" AND Degree > ANY(SELECT degree FROM score2 WHERE Cno="3-245");


#查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
SELECT * FROM score2 WHERE Cno="3-105" AND Degree > ALL(SELECT degree FROM score2 WHERE Cno="3-245");


#查询所有教师和同学的name、sex和birthday.
SELECT tname,tsex,tbirthday FROM teacher
UNION
SELECT sname,ssex,sbirthday FROM student2;


#查询所有“女”教师和“女”同学的name、sex和birthday.
SELECT tname,tsex,tbirthday FROM teacher WHERE Tsex="女"
UNION
SELECT sname,ssex,sbirthday FROM student2 WHERE ssex="女";


#查询成绩比该课程平均成绩低的同学的成绩表。

 

#查询所有有任课教师的Tname和Depart.
SELECT DISTINCT(cno) FROM score2;
SELECT Tno FROM course WHERE Cno in(SELECT DISTINCT(cno) FROM score2);
SELECT Tname,Depart FROM teacher WHERE Tno in(SELECT Tno FROM course WHERE Cno in(SELECT DISTINCT(cno) FROM score2));

select Tname,Depart from Teacher where Tname in (select distinct Tname from Teacher,Course,score2 where Teacher.Tno=Course.Tno and Course.Cno=score2.Cno);

#查询所有未讲课的教师的Tname和Depart.
select Tname,Depart from Teacher where Tname NOT in (select distinct Tname from Teacher,Course,score2 where Teacher.Tno=Course.Tno and Course.Cno=score2.Cno);

SELECT Tname,Depart FROM teacher WHERE Tno NOT in(SELECT Tno FROM course WHERE Cno in(SELECT DISTINCT(cno) FROM score2));
#查询至少有2名男生的班号。

 

#查询Student表中不姓“王”的同学记录。

 

#查询Student表中每个学生的姓名和年龄。

 

#查询Student表中最大和最小的Sbirthday日期值。

 

#以班号和年龄从大到小的顺序查询Student表中的全部记录。

 

#查询“男”教师及其所上的课程。

 

#查询最高分同学的Sno、Cno和Degree列。

 

#查询和“李军”同性别的所有同学的Sname.

 

#查询和“李军”同性别并同班的同学Sname.

 

#查询所有选修“计算机导论”课程的“男”同学的成绩表。


#分组函数avg,查询每门分数的平均分
SELECT * FROM score2 WHERE Cno="3-105";
#查询3-105科目的平均分
SELECT avg(degree) FROM score2 WHERE Cno="3-105";
#使用分组,按照cno课程分组,查询每个课程的avu(degree)平均分
SELECT avg(degree),cno FROM score2 GROUP BY Cno;

#查询每个学生的平均分
SELECT AVG(degree),student2.Sno FROM student2,score2 WHERE student2.Sno=score2.Sno GROUP BY student2.Sno;
#查询出男同学的平均分
SELECT sno FROM student2 WHERE ssex="男";

SELECT AVG(degree) FROM score2 WHERE sno in (SELECT sno FROM student2 WHERE ssex="男");

#查询出每个科目男同学的平均分


#查询出每门科目的最高分
SELECT MAX(degree),cno FROM score2 GROUP BY cno;

#查询女同学中的总分的最高分
SELECT sno FROM student2 WHERE ssex="女";

SELECT SUM(degree),sno FROM score2 WHERE Sno in(SELECT sno FROM student2 WHERE ssex="女") GROUP BY sno ORDER BY SUM(degree) DESC LIMIT 1;


#查询3-105的最低分
SELECT MIN(degree) FROM score2 WHERE Cno="3-105";

#查询所有分数对应的学生信息,课程编号,教师信息

 

以上是关于MySQL基础(查)的主要内容,如果未能解决你的问题,请参考以下文章

MySQL表的增删改查(基础)

MySQL增删改查基础知识

MySQL 增删改查基础

mysql 基础1---安装增删改查

MySQL增删查改 练习(基础)

MySQL基础(查)