创建如上数据库,并录入数据
1、自行创建测试数据
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`caption` varchar(32) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `class`
-- ----------------------------
BEGIN;
INSERT INTO `class` VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班');
COMMIT;
-- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(32) NOT NULL,
`teacher_id` int(11) NOT NULL,
PRIMARY KEY (`cid`),
KEY `fk_course_teacher` (`teacher_id`),
CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `course`
-- ----------------------------
BEGIN;
INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '体育', '3'), ('4', '美术', '2');
COMMIT;
-- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`num` int(11) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_score_student` (`student_id`),
KEY `fk_score_course` (`course_id`),
CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `score`
-- ----------------------------
BEGIN;
INSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');
COMMIT;
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gender` char(1) NOT NULL,
`class_id` int(11) NOT NULL,
`sname` varchar(32) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_class` (`class_id`),
CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `student`
-- ----------------------------
BEGIN;
INSERT INTO `student` VALUES ('1', '男', '1', '理解'), ('2', '女', '1', '钢蛋'), ('3', '男', '1', '张三'), ('4', '男', '1', '张一'), ('5', '女', '1', '张二'), ('6', '男', '1', '张四'), ('7', '女', '2', '铁锤'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '刘三'), ('14', '男', '3', '刘一'), ('15', '女', '3', '刘二'), ('16', '男', '3', '刘四');
COMMIT;
-- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(32) NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `teacher`
-- ----------------------------
BEGIN;
INSERT INTO `teacher` VALUES ('1', '张磊老师'), ('2', '李平老师'), ('3', '刘海燕老师'), ('4', '朱云海老师'), ('5', '李杰老师');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
表结构和数据
2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
SELECT * FROM
(SELECT score.student_id,course.cname,score.num FROM score LEFT JOIN course on score.course_id=course.cid WHERE course.cname="生物") AS A
INNER JOIN
(SELECT score.student_id,course.cname,score.num FROM score LEFT JOIN course on score.course_id=course.cid WHERE course.cname="物理") AS B
on A.student_id = B.student_id WHERE A.num > B.num;
3、查询平均成绩大于60分的同学的学号和平均成绩;
SELECT B.student_id,student.sname,B.s_num FROM (SELECT student_id,avg(num) AS s_num from score GROUP BY student_id HAVING s_num>60) as B
LEFT JOIN student on B.student_id = student.sid; 取前三: %ORDER BY s_num desc LIMIT 3;
4、查询所有同学的学号、姓名、选课数、总成绩;
SELECT score.student_id,student.sname,COUNT(score.course_id) AS courses,SUM(num) as T_Score FROM score
LEFT JOIN student on score.student_id=student.sid GROUP BY score.student_id;
5、查询姓“李”的老师的个数;
SELECT * from teacher WHERE tname like "李%";
6、查询没学过“李平”老师课的同学的学号、姓名;
SELECT student.sid,student.sname FROM student WHERE student.sid NOT IN (
SELECT score.student_id FROM score LEFT JOIN course ON score.course_id = course.cid WHERE score.course_id IN (
SELECT course.cid FROM course LEFT JOIN teacher ON course.teacher_id = teacher.tid WHERE teacher.tname LIKE "李平%") GROUP BY score.student_id)
7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
SELECT B.sid,B.sname FROM(
SELECTstudent.sid,score.course_id,student.sname FROM score
LEFT JOIN student ON score.student_id = student.sid WHERE score.course_id BETWEEN 1 AND 2) AS B
GROUP BY B.sid HAVING COUNT(B.course_id)=2;
8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
SELECT student.sid,student.sname FROM score LEFT JOIN student ON score.student_id=student.sid WHERE score.course_id in
(SELECT course.cid FROM course LEFT JOIN teacher on course.teacher_id=teacher.tid WHERE tname LIKE "李平%")
GROUP BY student.sid HAVING COUNT(student_id)=2;
9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
SELECT A.sid,A.sname,A.num as score_2,B.num as score_1 FROM
(SELECT student.sid,student.sname,score.num FROM score LEFT JOIN student on score.student_id=student.sid WHERE score.course_id=2) AS A
INNER JOIN
(SELECT student.sid,student.sname,score.num FROM score LEFT JOIN student on score.student_id=student.sid WHERE score.course_id=1) AS B
ON A.sid = B.sid WHERE A.num<B.num;
10、查询有课程成绩小于60分的同学的学号、姓名;
SELECT student.sid,student.sname FROM student WHERE sid IN
(SELECT score.student_id FROM score WHERE score.num<60 GROUP BY student_id);
11、查询没有学全所有课的同学的学号、姓名;
SELECT student.sid,student.sname FROM student WHERE sid in
(SELECT student_id FROM score GROUP BY student_id HAVING count(course_id)!=(SELECT COUNT(cid) FROM course));
12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
SELECT student.sid,student.sname FROM student where student.sid in
(SELECT score.student_id FROM score WHERE course_id in
(SELECT score.course_id FROM score WHERE student_id=1) and student_id !=1 GROUP BY student_id);
13、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;
SELECT student.sid,student.sname FROM student where student.sid in
(SELECT score.student_id FROM score WHERE course_id in
(SELECT score.course_id FROM score WHERE student_id=1) and student_id !=1 GROUP BY student_id
having count(1) =(SELECT count(score.course_id) FROM score WHERE student_id=1));
14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
SELECT sid AS 学号,sname AS 姓名 FROM student WHERE sid IN(
SELECT student_id FROM score WHERE student_id IN (
SELECT student_id FROM score WHERE student_id!=2 GROUP BY student_id
HAVING COUNT(1)=(SELECT COUNT(1) FROM score WHERE student_id=2))
AND course_id in (SELECT course_id FROM score WHERE student_id=2)
GROUP BY student_id HAVING COUNT(1)=(SELECT COUNT(1) FROM score WHERE student_id=2))
15、删除学习“叶平”老师课的SC表记录;
DELETE FROM score WHERE course_id IN
(SELECT cid FROM course LEFT JOIN teacher on course.teacher_id=teacher.tid WHERE tname="李平老师")
16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
INSERT into score(student_id,course_id,num)
SELECT student_id,2,(SELECT AVG(num) FROM score WHERE course_id=2) FROM score WHERE course_id!=2 group by student_id
SELECT AVG(num) FROM score WHERE course_id=2 --
17、 按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
SELECT student_id AS 学生ID,
(SELECT num FROM score LEFT JOIN course on score.course_id=course.cid WHERE course.cname="语文" AND student_id=s1.student_id) AS 语文,
(SELECT num FROM score LEFT JOIN course on score.course_id=course.cid WHERE course.cname="数学" AND student_id=s1.student_id) AS 数学,
(SELECT num FROM score LEFT JOIN course on score.course_id=course.cid WHERE course.cname="英语" AND student_id=s1.student_id) AS 英语
FROM score as s1 GROUP BY student_id;
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
SELECT score.course_id AS 课程ID,course.cname as 课程名,MAX(num) as 最高分,MIN(num) as 最低分 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY course_id;
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
平均分:
SELECT score.course_id AS 课程ID,course.cname as 课程名,AVG(num) AS 平均分 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY score.course_id ORDER BY 平均分 ASC;
及格率:
SELECT A.cid,A.cname,ROUND(B.s/A.ss*100,1)+% AS 及格率 FROM
(SELECT cid,cname,COUNT(course_id) AS ss FROM score LEFT JOIN course ON score.course_id=course.cid GROUP BY course_id) as A
INNER JOIN
(SELECT course_id,COUNT(num) as s FROM score WHERE score.num>60 GROUP BY course_id) as B ON A.cid=B.course_id ORDER BY 及格率 DESC;
整合:
SELECT x.课程ID,x.课程名,x.平均分,xx.及格率 FROM
(SELECT score.course_id AS 课程ID,course.cname as 课程名,AVG(num) AS 平均分 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY score.course_id ORDER BY 平均分 ASC) AS x
LEFT JOIN
(SELECT A.cid,A.cname,ROUND(B.s/A.ss*100,1) AS 及格率 FROM
(SELECT cid,cname,COUNT(course_id) AS ss FROM score LEFT JOIN course ON score.course_id=course.cid GROUP BY course_id) as A
INNER JOIN
(SELECT course_id,COUNT(num) as s FROM score WHERE score.num>60 GROUP BY course_id) as B ON A.cid=B.course_id ORDER BY 及格率 DESC) AS xx
ON x.课程ID=xx.cid
标准:
SELECT course_id,AVG(num) AS 平均分,ROUND(sum(CASE WHEN num<60 then 0 ELSE 1 END)/sum(1)*100,1) AS 及格率
FROM score GROUP BY course_id ORDER BY 平均分 ASC,及格率 DESC;
20、课程平均分从高到低显示(现实任课老师);
SELECT course.cname,teacher.tname,xxx.平均分 FROM course
LEFT JOIN teacher ON course.teacher_id=teacher.tid
LEFT JOIN (SELECT course_id,AVG(num) AS 平均分 FROM score GROUP BY course_id) AS xxx ON xxx.course_id=course.cid
ORDER BY xxx.平均分 DESC
21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT s1.cid AS 课程ID,s1.cname AS 课程,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 0,1) AS 第一,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 1,1) AS 第二,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 2,1) AS 第三
FROM course AS s1
22、查询每门课程被选修的学生数;
SELECT course.cid AS 课程ID,course.cname AS 课程名,COUNT(1) AS 学生数 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY score.course_id;
23、查询出只选修了一门课程的全部学生的学号和姓名;
SELECT student.sid AS 学号,student.sname AS 姓名 FROM student
WHERE sid IN (SELECT student_id FROM score GROUP BY student_id HAVING count(1)=1)
24、查询男生、女生的人数;
SELECT gender AS 性别,COUNT(1) AS 人数 FROM student GROUP BY gender;
25、查询姓“张”的学生名单;
SELECT * FROM student WHERE sname LIKE "张%"
26、查询同名同姓学生名单,并统计同名人数;
SELECT sname As 姓名,COUNT(1) AS 人数 FROM student GROUP BY sname;
27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
SELECT
course.cid AS 课程ID,
course.cname AS 课程名,
AVG(IF(ISNULL(num),0,score.num)) AS 平均分
FROM score LEFT JOIN course ON score.course_id = course.cid
GROUP BY score.course_id ORDER BY 平均分 ASC,课程ID DESC;
28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
SELECT student.sid AS 学号,student.sname AS 姓名,AVG(if(isnull(num),0,score.num)) AS 平均分
FROM score LEFT JOIN student ON score.student_id=student.sid
GROUP BY score.student_id HAVING 平均分>85;
29、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
SELECT student.sid AS 学号,student.sname AS 姓名,score.num AS 成绩 FROM score
LEFT JOIN course ON score.course_id=course.cid
LEFT JOIN student ON score.student_id= student.sid
WHERE course.cname="数学" AND score.num<60;
30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
SELECT student.sid AS 学号,student.sname AS 姓名 FROM student WHERE sid IN
(SELECT student_id FROM score WHERE score.course_id =3 AND num > 80)
31、求选了课程的学生人数
SELECT COUNT(A.student_id) AS 总人数
FROM (SELECT student_id FROM score GROUP BY student_id) AS A
SELECT COUNT(DISTINCT student_id) AS 总人数 FROM score;
32、查询选修“杨艳”老师(这个老师没有,就以张磊老师举例)所授课程的学生中,成绩最高的学生姓名及其成绩;
SELECT student.sid AS 学号,student.sname AS 姓名,num AS 成绩 FROM score
LEFT JOIN course ON score.course_id=course.cid
LEFT JOIN student ON score.student_id=student.sid
LEFT JOIN teacher ON course.teacher_id=teacher.tid
WHERE teacher.tname = "张磊老师" ORDER BY num DESC LIMIT 1;
33、查询各个课程及相应的选修人数;
SELECT course_id AS ID,course.cname AS 课程,count(1) AS 人数 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY course_id
34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
SELECT DISTINCT s1.student_id,s1.course_id,s1.num FROM score AS s1,score AS s2
WHERE s1.student_id != s2.student_id AND s1.course_id!=s2.course_id AND s1.num=s2.num
35、查询每门课程成绩最好的前两名;
SELECT cid AS 课程ID,cname AS 课程,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 0,1) AS 第一,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 1,1) AS 第二
FROM course AS s1
36、检索至少选修两门课程的学生学号;
SELECT student_id FROM score GROUP BY student_id HAVING count(course_id)>=2
37、查询全部学生都选修的课程的课程号和课程名;
SELECT course.cid AS 课程号,course.cname AS 课程名 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY score.course_id
HAVING COUNT(student_id)=(SELECT COUNT(sid) FROM student)
38、查询没学过“李平”老师讲授的任一门课程的学生姓名;
SELECT student.sid,student.sname FROM student
WHERE student.sid not IN (SELECT student_id FROM score
WHERE course_id IN(SELECT cid FROM course
LEFT JOIN teacher ON course.teacher_id=teacher.tid
WHERE teacher.tname="李平老师" ) GROUP BY student_id)
39、查询两门以上不及格课程的同学的学号及其平均成绩;
SELECT score.student_id AS 学号,student.sname AS 姓名,
(SELECT AVG(if(ISNULL(A.num),0,A.num)) FROM score AS A
WHERE A.student_id IN (SELECT student_id FROM score WHERE num<60
GROUP BY student_id HAVING COUNT(1)>=2)) AS 平均成绩
FROM score LEFT JOIN student ON score.student_id=student.sid
WHERE num<60 GROUP BY student_id HAVING COUNT(1)>2
标准:
SELECT student_id AS 学号,sname AS 姓名,AVG(num) AS 平均成绩 FROM score
LEFT JOIN student ON score.student_id=student.sid
WHERE student_id IN (SELECT student_id FROM score
WHERE num<60 GROUP BY student_id HAVING COUNT(1)>=2)
GROUP BY student_id
40、检索“004”课程分数小于60,按分数降序排列的同学学号;
SELECT student_id,num FROM score
WHERE score.course_id=4 AND num <60 ORDER BY num ASC
41、删除“002”同学的“001”课程的成绩;
delete from score where student_id=2 and corse_id=1;
NAVICAT
从navicat官网下载试用客户端即可
png格式即可,用来创建linux桌面图标
安装
- 将下载的安装包节解压后,将解压出来的文件加移动到/opt目录中即完成安装
tar -zxvf '/home/philos/下载/navicat121_premium_cs_x64.tar.gz'
sudo mv '/home/philos/navicat121_premium_cs_x64' /opt/nvc
一般是/opt 或/usr/local,此处我放入/opt目录中
创建桌面图标
复制下载的02.Product_00_AllProducts_Premium_large.png为/opt/nvc/navicat.png目录中
创建桌面连接文件
sudo nano /usr/share/applications/Navicat.desktop
添加如下内容
[Desktop Entry]
Type=Application
Name=Navicat
GenericName=Navicat pro
Comment=Navicat:The SQL manager
Exec=sh /opt/nvc/start_navicat
Icon=/opt/nvc/navicat.png
Terminal=navicat
Categories=Navicat;Development;
在应用搜索页面navicat即可看到Navicat的图标
使用
双击图标打开后点击试用即可试用
中文版打开后中文乱码问题
修改/opt/nvc/start_navicat文件
export LANG="en_US.UTF-8"
修改为
- export LANG="zh_CN.UTF-8"
无限试用
打开navicat客户端,通过左上角文件备份链接
关闭打开的客户端
删除当前用户根目录下的navicat配置文件
rm -rf ~/.navicat64
肯定是.navicat开头的
重新打开nvicat客户端,会发现试用期往后延长了
通过navicat客户端左上角文件导入备份链接即可#linux下安装navicat并生成桌面图标
网友评论