码迷,mamicode.com
首页 > 数据库 > 详细

mysql 常用sql语句总结

时间:2018-12-24 02:28:17      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:cno   顺序   now()   重复   子查询   HERE   like   class   职称   

1、 查询Student表中的所有记录的Sname、Ssex和Class列。

SELECT Sname,Ssex,Class FROM student
select sname,ssex,class from student

2、 查询教师所有的单位即不重复的Depart列。

SELECT DISTINCT Depart FROM teacher
select distanct depart from teacher;
3、 查询Student表的所有记录。

SELECT * FROM student
select * from student
4、 查询Score表中成绩在60到80之间的所有记录。

SELECT * FROM Score WHERE Degree BETWEEN 60 AND 80

select * form score where degree between 60 and 80

5、 查询Score表中成绩为85,86或88的记录。

SELECT * FROM Score WHERE Degree IN(85,86,88)
select * from score where degree in(85,86,88)

6、 查询Student表中“95031”班或性别为“女”的同学记录。

SELECT * FROM Student WHERE class=‘95031‘ OR Ssex=‘女‘

welect * from student where class=‘95031‘ or ssex=‘女‘
7、 以Class降序查询Student表的所有记录。

SELECT * FROM student ORDER BY class DESC
select * from student order by class desc/asc


8、 以Cno升序、Degree降序查询Score表的所有记录。

SELECT * FROM Score ORDER BY cno ASC,degree DESC
select * from score order by cno asc,degree desc
9、 查询“95031”班的学生人数。

SELECT COUNT(*) FROM student WHERE class=‘95031‘
10、查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

SELECT Sno,Cno FROM Score WHERE Degree=(SELECT MAX(Degree) FROM Score);
SELECT Sno,Cno FROM Score ORDER BY Degree DESC LIMIT 0,1;
11、查询每门课的平均成绩。

SELECT Cno,AVG(degree) FROM Score GROUP BY Cno;
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

SELECT AVG(Degree) FROM score WHERE Cno LIKE ‘3%‘ AND Cno IN (SELECT Cno FROM score GROUP BY Cno HAVING COUNT(*)>=5) 用in 不用= 是因为可能会有多个
简单写法:select AVG(Degree) FROM score WHERE Cno LIKE ‘3%‘ AND GROUP BY Cno HAVING COUNT(*)>=5;
13、查询分数大于70,小于90的Sno列。

SELECT Sno FROM Score WHERE degree>70 AND degree<90;
14、查询所有学生的Sname、Cno和Degree列。

SELECT Sname, Cno,Degree FROM Score , student WHERE Score.Sno=student.Sno;
15、查询所有学生的Sno、Cname和Degree列。

SELECT Sno,Cname,Degree FROM Score , Course WHERE Score.Cno=Course.Cno;
16、查询所有学生的Sname、Cname和Degree列。

SELECT Sname,Cname,Degree FROM student,course,score WHERE student.Sno=score.Sno AND course.Cno=score.Cno
JOIN .. ON 写法:select Sname,Cname,Degree FROM student JOIN score ON student.Sno=score.Sno JOIN course ON course.Cno=score.Cno;
17、查询“95033”班学生的平均分。

SELECT AVG(degree) AS ‘class=95033‘ FROM Score WHERE Sno IN (SELECT Sno FROM Student WHERE Class=‘95033‘ );  
18、 假设使用如下命令建立了一个grade表:

CREATE TABLE grade(low INT(3),upp INT(3),rank CHAR(1))

INSERT INTO grade VALUES(90,100,’A’)

INSERT INTO grade VALUES(80,89,’B’)

INSERT INTO grade VALUES(70,79,’C’)

INSERT INTO grade VALUES(60,69,’D’)

INSERT INTO grade VALUES(0,59,’E’)

现查询所有同学的Sno、Cno和rank列。

SELECT Sno,Cno,rank FROM Score,grade WHERE degree BETWEEN low AND upp;
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

<span style="color: #000000; font-size: 15px">109同学,选修是3-105课的</span>
SELECT * FROM score WHERE Cno=‘3-105‘ AND degree>(SELECT MAX(degree ) FROM Score WHERE Sno=‘109‘ AND Cno=‘3-105‘ )
<span style="font-size: 15px">109同学,没有选修3-105课</span>
SELECT * FROM score WHERE Cno=‘3-105‘ AND degree>(SELECT MAX(degree ) FROM Score WHERE Sno=‘109‘)
AND degree<( SELECT MAX(degree ) FROM Score WHERE sno IN (SELECT Sno FROM score GROUP BY Sno HAVING COUNT(*)>1))

20、选了多门课程并且是这个课程下不是最高分的
SELECT * FROM score a WHERE Sno IN (SELECT Sno FROM score GROUP BY Sno HAVING COUNT(*)>1) AND degree<( SELECT MAX(degree ) FROM Score b WHERE b.cno = a.cno)
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

SELECT * FROM score WHERE degree>(SELECT degree FROM Score WHERE Sno=‘109‘ AND Cno=‘3-105‘ )
22、查询和学号为108、101的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

SELECT sno,sname,sbirthday FROM student WHERE YEAR(sbirthday) = (SELECT YEAR(sbirthday) FROM student WHERE sno=‘108‘)
SELECT sno,sname,sbirthday FROM student WHERE YEAR(sbirthday) = (SELECT YEAR(sbirthday) FROM student WHERE sno=‘101‘)
23、查询“张旭“教师任课的学生成绩。

SELECT Sno,degree FROM score,Course WHERE score.Cno=Course.Cno AND Course.Tno= (SELECT Tno FROM Teacher WHERE Tname=‘张旭‘ )
SELECT degree FROM score WHERE Cno IN (SELECT cno FROM course WHERE Tno= (SELECT Tno FROM Teacher WHERE Tname=‘张旭‘ ) )
24、查询选修某课程的同学人数多于5人的教师姓名。

SELECT Tname FROM Teacher, Course WHERE Teacher.Tno=Course.Tno AND Course.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 ))
25、查询95033班和95031班全体学生的记录。

SELECT * FROM student WHERE class IN (‘95033‘,‘95031‘)
26、 查询存在有85分以上成绩的课程Cno.

SELECT Cno FROM score WHERE degree>85
27、查询出“计算机系“教师所教课程的成绩表。

SELECT * FROM course WHERE cno IN (SELECT cno FROM course WHERE tno IN (SELECT tno FROM teacher WHERE Depart=‘计算机系‘))
28、查询“计算 机系”与“电子工程系“不同职称的教师的Tname和Prof。

SELECT Tname,Prof FROM Teacher WHERE Depart =‘计算机系‘ AND Prof NOT IN( SELECT Prof FROM Teacher WHERE Depart =‘电子工程系‘)<br>UNION <br>SELECT Tname,Prof FROM Teacher WHERE Depart =‘电子工程系‘ AND Prof NOT IN( SELECT Prof FROM Teacher WHERE Depart =‘计算机系‘)
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

ANY:代表括号中任意一个成绩就可以

SELECT Cno,Sno,Degree FROM score WHERE cno=‘3-105‘ AND degree >ANY(SELECT degree FROM score WHERE cno=‘3-245‘ ) ORDER BY degree DESC
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

ALL:代表括号中的所有成绩

SELECT Cno,Sno,Degree FROM score WHERE cno=‘3-105‘ AND degree >ALL(SELECT degree FROM score WHERE cno=‘3-245‘ ) ORDER BY degree DESC
31、 查询所有教师和同学的name、sex和birthday.

SELECT tname,tsex,tbirthday FROM Teacher UNION SELECT sname,ssex,sbirthday FROM Student
32、查询所有“女”教师和“女”同学的name、sex和birthday.

SELECT Tname,Tsex,Tbirthday FROM Teacher WHERE Tsex=‘女‘ UNION SELECT Sname,Ssex,Sbirthday FROM Student WHERE Ssex=‘女‘
33、 查询成绩比该课程平均成绩低的同学的成绩表。

SELECT * FROM score a WHERE degree < (SELECT AVG(degree) FROM score b WHERE b.cno=a.cno)
34、 查询所有任课教师的Tname和Depart.

SELECT Tname,Depart FROM Teacher WHERE tno IN (SELECT tno FROM course )
35 、 查询所有未讲课的教师的Tname和Depart.

SELECT Tname,Depart FROM Teacher WHERE Tno NOT IN (SELECT Tno FROM Course WHERE cno IN (SELECT cno FROM score ))
36、查询至少有2名男生的班号。

SELECT class FROM student WHERE ssex=‘男‘ GROUP BY class HAVING COUNT(*)>1
37、查询Student表中不姓“王”的同学记录。

SELECT * FROM Student WHERE Sname NOT LIKE ‘王%%‘
38、查询Student表中每个学生的姓名和年龄。

SELECT Sname, YEAR(NOW())-YEAR(sbirthday) FROM Student
39、查询Student表中最大和最小的Sbirthday日期值。

SELECT MAX(Sbirthday ),MIN(Sbirthday ) FROM Student
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

SELECT * FROM Student ORDER BY class DESC, Sbirthday
41、查询“男”教师及其所上的课程。

SELECT Tname,Cname FROM course,teacher WHERE course.tno= teacher.tno AND teacher.Tsex=‘男‘
42、查询最高分同学的Sno、Cno和Degree列。

SELECT Sno,Cno,Degree FROM score WHERE degree=(SELECT MAX(degree) FROM score)
排序写法:

SELECT Sno,Cno,Degree FROM score ORDER BY degree DESC LIMIT 0,1
43、查询和“李军”同性别的所有同学的Sname.

SELECT Sname FROM Student WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname=‘李军‘)
44、查询和“李军”同性别并同班的同学Sname.

SELECT Sname FROM Student WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname=‘李军‘ ) AND class=( SELECT class FROM student WHERE Sname=‘李军‘)
45、查询所有选修“计算机导论”课程的“男”同学的成绩表。

SELECT Sno,Cno,degree FROM score WHERE Cno=( SELECT Cno FROM course WHERE Cname=‘计算机导论‘) AND Sno IN (SELECT Sno FROM student WHERE Ssex=‘男‘)

mysql 常用sql语句总结

标签:cno   顺序   now()   重复   子查询   HERE   like   class   职称   

原文地址:https://www.cnblogs.com/qqzhulu/p/10166425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!