数据分组,需求里常见关键字:各,不同…
内连接:是多表的交集部分
外连接:通过外连接关键字指定的部分
左外/右外连接:(1)左外使用left outer join.. on 右外使用right outer join…on
(2)左外:以关键字左表为主,左表的字段全部显示,右外相反
Like %多个任意数据,_一个任意数据
Drop 一般用于删除数据库和数据表 ,是删除表结构 ,delete用于删除表数据内容
主键用于指示数据的唯一性。特征:唯一、非空
Limit ,limit 1, 6
Count(*) 针对非空或空数据都做计数。Count(字段)针对该字段非空的数据。
Order by关键字+排序的字段,asc升序(默认), desc降序
---------------------------------day1---------------------------------
#1.统计项目表中项目名称以“管理”开头的记录一共多少条
SELECT COUNT(*) FROM project p WHERE p.pname LIKE '管理%';
#2.查询出项目表中code内容长度大于11的所有记录
SELECT * FROM project p WHERE lengh(p.`porg_code`)>11;
#3.统计项目人员信息表中一共有多少条记录
SELECT COUNT(*) FROM project_user;
#4.统计项目人员信息表中一共有多少员工
SELECT COUNT(uname) FROM project_user;
#5.查询出哪个项目的参与人员数量最多
SELECT MAX(c.sers),p,name FROM
(SELECT p.name AS proname,COUNT(u.uer_id) AS users FROM project p INNER JOIN project_user u ON u.project_id=p.id AND u.type=3 GROUP BY u.uer_id) c;
---------------------------------day2---------------------------------
#学生表
CREATE TABLE Students(
Sid INT PRIMARY KEY, #学生学号
Sname VARCHAR(10) NOT NULL, #学生姓名
Sage INT, #学生年龄
Ssex NVARCHAR(5) #学生性别
) CHARSET utf8;
#教师表
CREATE TABLE Teacher(
Tid INT PRIMARY KEY, #教师号
Tname VARCHAR(10) NOT NULL #教师名字
) CHARSET utf8;
#课程表
CREATE TABLE Course(
Cid INT PRIMARY KEY, #课程ID
Cname VARCHAR(15), #课程名称
Cteacherid INT, #教师ID
CONSTRAINT Course_fk FOREIGN KEY (Cteacherid) REFERENCES Teacher(Tid)
) CHARSET utf8;
#成绩表
CREATE TABLE Record(
Rstuid INT NOT NULL, #学生学号
Rcouid INT NOT NULL, #课程ID
Rmark INT, #分数
CONSTRAINT RecordStu_fk FOREIGN KEY (Rstuid) REFERENCES Student(Sid),
CONSTRAINT RecordCou_fk FOREIGN KEY (Rcouid) REFERENCES Course(Cid)
) CHARSET utf8;
#4个表数据插入
INSERT INTO Students VALUES(001,"张三",20,"男");
INSERT INTO Students VALUES(002,"李四",24,"男");
INSERT INTO Students VALUES(003,"王五",24,"女");
INSERT INTO Students VALUES(004,"赵六",22,"男");
INSERT INTO Students VALUES(005,"孙七",21,"女");
INSERT INTO Students VALUES(006,"钱八",26,"男");
INSERT INTO Students VALUES(007,"吴用",27,"女");
SELECT * FROM Students;
INSERT INTO Teacher VALUES(1,"乐老师");
INSERT INTO Teacher VALUES(2,"郭老师");
INSERT INTO Teacher VALUES(3,"小美老师");
INSERT INTO Teacher VALUES(4,"贺老师");
SELECT * FROM Teacher;
INSERT INTO Course VALUES(1,"Python",1);
INSERT INTO Course VALUES(2,"MySQL",2);
INSERT INTO Course VALUES(3,"Java",3);
INSERT INTO Course VALUES(4,"Linux",4);
SELECT * FROM Course;
INSERT INTO Record VALUES(1,1,80);
INSERT INTO Record VALUES(1,2,90);
INSERT INTO Record VALUES(1,3,70);
INSERT INTO Record VALUES(1,4,78);
INSERT INTO Record VALUES(2,1,70);
INSERT INTO Record VALUES(2,2,95);
INSERT INTO Record VALUES(2,3,60);
INSERT INTO Record VALUES(2,4,75);
INSERT INTO Record VALUES(3,1,90);
INSERT INTO Record VALUES(3,2,85);
INSERT INTO Record VALUES(3,3,70);
INSERT INTO Record VALUES(3,4,85);
INSERT INTO Record VALUES(4,1,70);
INSERT INTO Record VALUES(4,2,40);
INSERT INTO Record VALUES(4,3,50);
INSERT INTO Record VALUES(4,4,55);
INSERT INTO Record VALUES(5,1,80);
SELECT * FROM Record;
SELECT * FROM Student;
SELECT * FROM Teacher;
SELECT * FROM Course; #课程表
SELECT * FROM Record; #成绩表
#查询平均成绩大于60分的同学的学号和平均成绩
SELECT r.`Rstuid` '学号',AVG(r.`Rmark`) '平均成绩'
FROM Record r GROUP BY r.`Rstuid` HAVING AVG(r.`Rmark`)>60;
#按各科平均成绩从低到高和及格率的百分数从高到低顺序查询
SELECT r.`Rcouid`,AVG(r.Rmark) avgrmark,
SUM(r.Rmark>=60)/COUNT(r.Rstuid) AS percent
FROM Record r
GROUP BY r.`Rcouid` ORDER BY avgrmark ASC, percent DESC;
#查询语文课程成绩在70分以上的学生姓名、课程名称和分数
SELECT s.`sname`,c.`Cname`,r.`Rmark`
FROM Student s INNER JOIN Course c INNER JOIN Record r
ON s.`id`=r.`Rstuid` AND c.`Cid`=r.`Rcouid`
AND c.`Cname`='语文' AND r.`Rmark`>70;
#删除002同学的001课程的成绩
DELETE FROM Record WHERE Rstuid='002' AND Rcouid='001';
#增加一条数据:学生为张三报考教师王老师数学学课程的考试成绩
#方法一:
SET @sid = (SELECT sid FROM Students WHERE sname='张三');
SET @tname = (SELECT t.tid FROM Teacher t,Course c
WHERE t.tname='贺老师' AND c.cname='MySQL'
AND c.Cteacherid=t.Tid);
INSERT INTO Record VALUES (@sid,@tname,99);
#方法二:
INSERT INTO Record VALUES
(SELECT sid FROM Students WHERE sname='张三',
SELECT t.tid FROM Teacher t,Course c
WHERE t.tname='贺老师' AND c.cname='MySQL'
AND c.Cteacherid=t.Tid,99);
---------------------------------day3---------------------------------
CREATE DATABASE information;
CREATE TABLE inf
(
iname VARCHAR(4),-- 姓名
applyno VARCHAR(4) PRIMARY KEY,-- 申请号
tel VARCHAR(11),-- 手机号
inf_id INT-- 证件号
)CHARSET utf8;
INSERT INTO inf VALUES
('张三',1001,'A001',1370755),
('李四',1004,'A002',1370756);
INSERT INTO inf VALUES
('陈六',1006,'A004',1320761),
('何七',1007,'A005',1340752),
('李四',1003,'A006',1370757),
('王五',1005,'A007',1370758);
SELECT * FROM inf;
CREATE TABLE apply
(
applyno VARCHAR(4),-- 申请号
custno VARCHAR(4),-- 客户号
aprocess VARCHAR(10),-- 申请过程
accto VARCHAR(5),-- 账户号
CONSTRAINT apply_fk FOREIGN KEY (applyno) REFERENCES inf(applyno)-- 外键必须是另一个表的主键或者唯一键;外键的字段类型必须和主键的字段类型一致
)CHARSET utf8;
INSERT INTO apply VALUES
('1001','C003','pass','TT003'),
('A002','C004','pass','TT004'),
('A003','C003','reject','TT003'),
('A004','C006','pass','TT006'),
('A005','C007','reject','TT007'),
('A006','C004','running','TT004'),
('A007','C005','running','TT005');
SELECT * FROM apply;
CREATE TABLE trans
(
custno VARCHAR(4),-- 客户号
amt INT,-- 交易金额
t_status VARCHAR(10),-- 状态
t_time DATETIME-- 时间
)CHARSET utf8;
INSERT INTO trans VALUES
('C003',500,'S00','2016-3-1 10:50');
INSERT INTO trans VALUES
('C004',400,'S00','2016-4-8 14:32'),
('C003',700,'F01','2016-4-9 12:41'),
('C006',214,'E23','2016-4-9 13:20'),
('C007',339,'F01','2015-12-21 19:52'),
('C004',780,'E99','2016-4-6 16:22'),
('C005',1100,'S00','2016-4-8 16:24');
SELECT * FROM trans;
-- 1.查询A表手机号不为空的所有信息
SELECT* FROM inf WHERE tel<>'' OR tel IS NOT NULL;
-- 2.查询张三这个客户发起的交易总额
#写法一: 内连接
SELECT i.`iname`,SUM(t.`amt`) '交易总额' FROM trans t, inf i,apply a
WHERE a.`applyno`=i.`applyno` AND t.`custno`=a.`custno` AND i.`iname`='张三';
#写法二:子查询
SELECT i.`iname`,SUM(amt) '交易总额' FROM trans WHERE custno=
(SELECT DISTINCT custno FROM inf,apply WHERE inf.`applyno`=apply.`applyno` AND iname='张三');
-- 3.查询手机号是137开头的人的姓名、申请号和客户号
SELECT i.iname '姓名',i.`applyno` '申请号',a.custno '客户号'
FROM inf i,apply a
WHERE i.`applyno`=a.`applyno` AND tel LIKE '137%';
-- 4.查询交易金额小于1000的数据,并按时间降序排列
SELECT * FROM trans WHERE amt<1000 ORDER BY t_time DESC;
-- 5.把C表中状态为E99和E23的改为S00
UPDATE trans SET t_status='S00'
WHERE t_status IN('E99','E23');
------------------------day4-----------------------
CREATE TABLE Students
(
id INT,
sno VARCHAR(10) PRIMARY KEY,
sname VARCHAR(20),
sage INT,
ssex VARCHAR(3)
) CHARSET=utf8;
CREATE TABLE Mark
(
id INT PRIMARY KEY,
Sno_M VARCHAR(10),
Cname VARCHAR(10),
Scroe INT,
CONSTRAINT mark_fk FOREIGN KEY (Sno_M) REFERENCES Studentss (sno)
) CHARSET=utf8;
INSERT INTO Students VALUES(1,"SA001","张三",16,"男");
INSERT INTO Students VALUES(2,"SA002","李四",15,"女");
INSERT INTO Students VALUES(3,"SA003","王二",14,"男");
INSERT INTO Mark VALUES(1,"SA001","语文",96);
INSERT INTO Mark VALUES(2,"SA002","数学",89);
INSERT INTO Mark VALUES(3,"SA003","英语",90);
INSERT INTO Mark VALUES(4,"SA002","语文",88);
INSERT INTO Mark VALUES(5,"SA001","数学",89);
INSERT INTO Mark VALUES(6,"SA003","语文",78);
#查询平均成绩大于60分的同学的学号和平均成绩;
SELECT m.Sno_M,AVG(m.scroe) FROM mark m GROUP BY m.Sno_M HAVING AVG(m.scroe)>60
#查询平均成绩大于60分的同学的姓名和平均成绩;
SELECT s.Sname,AVG(m.scroe) FROM student s,mark m WHERE s.sno=m.Sno_M
GROUP BY m.Sno_M HAVING AVG(m.scroe)>60
#查询”语文”比”数学”高的所有学生的学号
SELECT scroe1.Sno_M FROM
(SELECT * FROM mark WHERE Cname="语文") scroe1,
(SELECT * FROM mark WHERE Cname="数学") scroe2
WHERE scroe1.Scroe>scroe2.Scroe AND scroe1.Sno_M=scroe2.Sno_M
#查询”语文”比”数学”高的所有学生的姓名
SELECT s.sname,scroe1.Sno_M FROM
student s,
(SELECT * FROM mark WHERE Cname="语文") scroe1,
(SELECT * FROM mark WHERE Cname="数学") scroe2
WHERE scroe1.Scroe>scroe2.Scroe AND scroe1.Sno_M=scroe2.Sno_M
AND s.sno=scroe1.Sno_M
-----------------------day5------------------------
SELECT * FROM `students`;
SELECT * FROM `teacher`;
SELECT * FROM `course`;
#查询学生表每个年纪有多少人,并按年级人数从多到少排序,查询出的列名为年级人数
SELECT grade,COUNT(*) 'nums' FROM students GROUP BY grade ORDER BY nums DESC;
#查询每门课程对应的年级和对应的教师,对于没有老师教的课程,也需要展示出来
SELECT c.`Cname`,t.`Tname`,c.`grade` FROM course c LEFT JOIN teacher t
ON c.`Cteacherid`=t.`Tid`;
看到这里,如果你有一颗想要顺利进阶,升职加薪的❤️不妨参与我们的软件测试一周集训吧!可以后台私信我,也可以进入qun :⑥③⑥⑧⑤⑨⑨⑥④,获取更多咨询哦~