mysql

Wesley13
• 阅读 447
--创造学生情况表并添加数据CREATE TABLE student(
     Sno VARCHAR(20) PRIMARY KEY,
     Sname VARCHAR(20) NOT NULL,
     Ssex  VARCHAR(20) NOT NULL,
     Sbirthday DATETIME,
     Class VARCHAR(20)
)
DELETE FROM student;
SELECT * FROM student;
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);
--创造课程表并添加数据
CREATE TABLE course(
 Cno VARCHAR(20) PRIMARY KEY,
 Cname VARCHAR(20) NOT NULL,
 Tno VARCHAR(20) NOT NULL
 )
 
 SELECT * FROM course;
 
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);
--创造课程表并添加数据 
 CREATE TABLE score(
   Sno VARCHAR(20) NOT NULL,
   Cno VARCHAR(20) NOT NULL,
   Degree DECIMAL(4,1) 
 )
 
DELETE FROM score ;
SELECT * FROM score;

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('101','3-105',64);
INSERT INTO score VALUES('107','3-105',91);
INSERT INTO score VALUES('108','3-105',78);   
INSERT INTO score VALUES('101','6-166',85);  
INSERT INTO score VALUES('107','6-166',79);   
INSERT INTO score VALUES('108','6-166',81); 
 
--创造教师情况表并添加数据
 CREATE TABLE teacher(
     Tno VARCHAR(20) PRIMARY KEY,
     Tname VARCHAR(20) NOT NULL,
     Tsex VARCHAR(20) NOT NULL,
     Tbirthday DATETIME,
     Prof VARCHAR(20),
     Depart VARCHAR(20) NOT NULL
     ); 
 
 SELECT * FROM teacher;
 
 INSERT INTO teacher VALUES('804','李诚','男','1985-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','助教','电子工程系');
 
 
--  1、 查询Student表中的所有记录的Sname、Ssex和Class列。
SELECT Sname,Ssex,Class FROM student;

-- 2、 查询教师所有的单位即不重复的Depart列。
SELECT DISTINCT depart FROM teacher;

-- 3、 查询Student表的所有记录。
SELECT * FROM student;
-- 4、 查询Score表中成绩在60到80之间的所有记录。
SELECT * FROM score WHERE degree > 60 AND degree < 80;
-- 5、 查询Score表中成绩为85,86或88的记录。
SELECT * FROM score WHERE degree =85 OR degree =86 OR degree =88;
-- 6、 查询Student表中“95031”班或性别为“女”的同学记录。
--  以Class降序查询Student表的所有记录。
--  以Sno升序、Degree降序查询Score表的所有记录。
--  查询“95031”班的学生人数。
 SELECT * FROM student WHERE class = '95031' OR Ssex = '女';
 SELECT * FROM student ORDER BY class DESC;
 SELECT * FROM score ORDER BY cno ASC,degree DESC ;
 SELECT COUNT(class) FROM student WHERE class = '95031' ;
 
--  10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
--  查询每门课的平均成绩。
SELECT sno,cno FROM score WHERE degree =(SELECT MAX(degree) FROM score);
SELECT AVG(degree),cno FROM score GROUP BY cno;

-- 12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
-- 查询分数大于70,小于90的Sno列。
-- 查询所有学生的Sname、Cno和Degree列。
SELECT AVG(degree) FROM score GROUP BY cno HAVING cno LIKE '3%' AND COUNT(cno)>5;
SELECT sno FROM score WHERE degree>70 AND degree<90;
SELECT sname,cno,degree FROM score JOIN student ON score.`Sno` = student.`Sno` ;
-- 15、查询所有学生的Sno、Cname和Degree列。
SELECT sno,cname,degree FROM score JOIN course ON score.`Cno` = course.`Cno`;
-- 16、查询所有学生的Sname、Cname和Degree列。
SELECT sname,cname,degree FROM (student JOIN score ON student.sno =  score.`Sno`) JOIN course ON score.`Cno`=course.`Cno`;
-- 17、 查询“95033”班学生的平均分。
--  查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
SELECT AVG(degree) FROM score JOIN student ON score.`Sno` = student.`Sno` WHERE class = '95033';
SELECT * FROM score JOIN student ON score.`Sno` = student.`Sno`  WHERE cno = '3-105' AND degree>(SELECT degree FROM score WHERE sno='109' AND cno='3-105' );

-- 20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
  SELECT * FROM score  WHERE degree <> ALL(SELECT MAX(degree) FROM score GROUP BY cno) AND 
  sno IN(SELECT sno FROM score GROUP BY sno HAVING COUNT(sno)>1);     
--   21、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。     
SELECT * FROM score WHERE degree>(SELECT degree FROM score WHERE sno = '109' AND cno ='3-105');      
-- 22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
SELECT sno,sname,sbirthday FROM student WHERE YEAR(sbirthday) = (SELECT YEAR(sbirthday) FROM student WHERE sno = '108');
-- 23、查询“张旭“教师任课的学生成绩(姓名)。
-- 1、从成绩表 查询学生成绩  
-- 
-- 2、条件是这个课程是 “张旭“教师任课
-- A)从老师表 中查名字叫 张旭 编号
-- B)根据老师编号 在课程表中找到对应的 课程号
SELECT sname,degree FROM student JOIN score ON student.`Sno`=score.`Sno`
WHERE cno =( SELECT cno FROM course WHERE tno = (SELECT tno FROM teacher WHERE tname = '张旭'));-- 查询考计算机导论的学生成绩SELECT degree FROM score JOIN course ON score.cno = course.cno WHERE cname = '计算机导论';-- 查询李诚老师教的课程名称SELECT cname FROM course WHERE tno = (SELECT tno FROM teacher WHERE tname ='李诚');-- 教高等数学的老师是哪个系的SELECT depart FROM teacher WHERE tno =(SELECT tno FROM course WHERE cname ='高等数学');-- 24、查询选修某课程的同学人数多于5人的教师姓名。SELECT tname FROM teacher WHERE tno =(SELECT tno FROM course WHERE cno = (SELECT cno FROM score GROUP BY cno HAVING COUNT(cno)>5));   -- 25、查询95033班和95031班全体学生的记录。SELECT * FROM student WHERE class ='95033' OR class ='95031';-- 26、  查询存在有85分以上成绩的课程Cno.SELECT cno FROM score WHERE degree>85;-- 27、查询出“计算机系“教师所教课程的成绩表。SELECT degree FROM scoreWHERE cno IN (SELECT cno FROM course WHERE tno IN (SELECT tno FROM teacher WHERE depart = '计算机系'));-- 29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学    -- 的Cno、Sno和Degree,并按Degree从高到低次序排序。SELECT * 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 DISTINCT sname AS NAME,ssex AS sex,sbirthday AS birthday FROM student UNION SELECT tname,tsex,tbirthday FROM teacher;-- 32、查询所有“女”教师和“女”同学的name、sex和birthday.SELECT DISTINCT sname AS NAME,ssex AS sex,sbirthday AS birthday FROM student WHERE ssex ='女' UNION SELECT tname,tsex,tbirthday FROM teacher WHERE tsex = '女';-- 33、 查询成绩比该课程平均成绩低的同学的成绩表。SELECT * FROM Score a WHERE a.Degree<(SELECT AVG(Degree) FROM Score b WHERE a.Cno=b.Cno);-- 34、 查询所有任课教师的Tname和Depart.SELECT tname,depart FROM teacher;-- 35、查询所有未讲课的教师的Tname和Depart.SELECT tname,depart FROM teacher WHERE tno =(SELECT  tno FROM course LEFT JOIN score ON course.`Cno` = score.`Cno` WHERE degree IS NULL); -- 36、查询至少有2名男生的班号。SELECT class FROM student GROUP BY class  HAVING COUNT('男')>1;-- 37、查询Student表中不姓“王”的同学记录。SELECT * FROM student WHERE sname  NOT LIKE '王%';-- 38、查询Student表中每个学生的姓名和年龄。SELECT sname,(YEAR(CURRENT_DATE())-YEAR(Sbirthday)) FROM student;-- 39、查询Student表中最大和最小的Sbirthday日期值。SELECT MAX(DATE(sbirthday)),MIN(DATE(sbirthday)) FROM student;-- 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。SELECT * FROM student ORDER BY class,sbirthday DESC;-- 41、查询“男”教师及其所上的课程。SELECT tname,cname FROM teacher JOIN course ON teacher.`Tno`=course.`Tno` WHERE tsex = '男';-- 42、查询最高分同学的Sno、Cno和Degree列。SELECT * FROM score WHERE degree = (SELECT MAX(degree) FROM score);-- 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 * FROM score WHERE cno IN (SELECT cno FROM course WHERE cname = '计算机导论') AND sno IN (SELECT sno FROM student WHERE ssex = '男');
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
2个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
2年前
Mysql_项目1:group by和having的使用(删除重复的电子邮箱)
项目1:查找重复的电子邮箱创建email表,并插入如下三行数据\创建表CREATETABLEemail(idINTNOTNULLauto_increment,emailVARCHAR(20)NOTNULL,PRIMARYKEY(id));\插入数据
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
8个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这