一、添加
- 向stu表中添加一条学生信息
INSERT INTO stu VALUES(1008,'zhangsna12',12,'jining','123232465');
- 另一种方式
INSERT INTO stu(stuid,name,age,tel,address) VALUES(1002,'lisi',13,'31315313','jining');-- 建议用这种
INSERT INTO stu(name,age,tel,address) VALUES('1',13,'31315313','jining'),-- 建议用这种
('2',13,'31315313','jining');
二、修改
SELECt * from stu;
一定要加where条件,修改多个列用逗号隔开
UPDATe stu SET name = '李文英',address='山东' WHERe stuid=1001;
三、删除
DELETE FROM stu WHERe stuid=1008;
四、单表查询
1.简单查询
-- 1.查询所有学生信息
SELECt *FROM student;
-- 1.查询学生信息,显示学生姓名,电话,性别
SELECt name,phone,sex FROM student;-- name为关键字,所以要加引号区分
-- 3.查询哪些班级有学生,显示班级编号(去重:DISTINCT)
SELECt DISTINCT cid FROM student;
-- 4.使用算术运算符,as关键字用于重新命名,可加可不加
SELECt socre as 加分前的成绩, socre+10 加分后的成绩 FROM score;
练习1
-- 1.查询所有员工信息
SELECt *FROM employee;
-- 1.查询所有员工的姓名及对应工资
SELECt ename,salary FROM employee;
-- 3.过滤员工表中的重复数据
SELECt DISTINCT *FROM employee;
-- 4.查询出每位员工的工资,并加一
SELECt salary , salary + 1 FROM employee;
-- 5.使用别名表示姓名,职位,电话,工资
SELECt ename 姓名,phone 电话,salary 工资 FROM employee;
2.WHERe
-- 1.查询学号为4的学生信息
SELECt * FROM student WHERe stuid=4;
-- 2.查询考试成绩满足给定条件的学生学号,分数
SELECt stuid,socre FROM score WHERe socre>=60 AND socre<=80;
SELECt stuid,socre FROM score WHERe socre BETWEEN 60 AND 80;
SELECt stuid,socre FROM score WHERe socre=69 OR socre=79;
SELECt stuid,socre FROM score WHERe socre in(69,79);
3.NOT 非
% 代表多个字符
SELECt stuid,socre FROM score WHERe socre NOT BETWEEN 60 AND 80;
4.LIKE 像
SELECt * FROM student WHERe name LIKE '张%';
SELECt * FROM student WHERe name LIKE '%三';
SELECt * FROM student WHERe name LIKE '%m%';
_代表单个字符
SELECt * FROM student WHERe name LIKE '张_';
SELECt * FROM student WHERe name LIKE '张__';
5.is NULL
SELECt * FROM score WHERe socre IS NULL;
SELECt * FROM score WHERe socre IS NOT NULL;
练习2
SELECt * FROM employee WHERe salary>=5000;
SELECt * FROM employee WHERe salary>=3000 AND salary<=5000;
SELECt * FROM employee WHERe salary in (5000,3000,8000);
SELECt * FROM employee WHERe hiredate BETWEEN '2016-02-01' AND '2016-03-01';
SELECt * FROM employee WHERe manager is NULL;
SELECt ename,job,phone FROM employee WHERe ename LIKE '王%';
6. 聚合函数
- avg():求平均值
-- 求 score 成绩表中科目编号为1的所有学生平均成绩
SELECt AVG(socre) 平均成绩 FROM score WHERe subid=1;
- COUNT():求多少条记录
-- 用*表示所有列
SELECt COUNT(*) FROM score WHERe subid=1;
- SUM():求和
-- 求score成绩表中所有学生成绩的总和
SELECt SUM(socre) FROM score;
- MAX()和 MIN():求最值
SELECt MAX(socre) 最高分,MIN(socre)最低分 FROM score WHERe subid=2;
求已出成绩的各科的平均成绩
不使用分组
SELECt DISTINCT subid FROM score;
SELECt AVG(socre) FROM score WHERe subid=1;
SELECt AVG(socre) FROM score WHERe subid=2;
SELECt AVG(socre) FROM score WHERe subid=3;
- GROUP BY语句
使用分组(含有 每, 各,或带有聚合函数的情况)
-- mysql中可单独使用,分组后的列可以不出现在select中
SELECT AVG(socre) FROM score GROUP BY subid;
-- SQLServer中不可单独使用,需要结合having语句,而且分组后的列必须出现在select中
SELECT AVG(socre),subid FROM score GROUP BY subid;
查询出所有成绩加5分后,成绩还是小于60分
HAVINg针对查询结果
SELECt stuid,socre+5 加分后的成绩 FROM score HAVINg 加分后的成绩 <60;
WHERe只能针对表中真正存在的列进行筛选
SELECt stuid, socre FROM score WHERe socre <60;
-- 平均分大于70的科目
SELECt subid,AVG(socre) FROM score GROUP BY subid HAVINg AVG(socre)>70;
7. 排序
- ORDER BY语句
- asc 升序 (默认)
- desc 降序
SELECt * FROM student;
SELECt * FROM student ORDER BY cid,birthday ASC;
SELECt * FROM student ORDER BY phone DESC;
- LIMIT 语句,用于分页
LIMIT 数据的坐标,从0开始,显示的条数
SELECt * FROM student ORDER BY birthday ASC LIMIT 0,5;
练习3
SELECt * FROM employee ORDER BY salary ASC LIMIT 0,3;
SELECt * FROM employee ORDER BY did DESC, hiredate ASC, salary DESC;
SELECt * FROM employee ORDER BY salary DESC LIMIT 2,3;
五、多表查询
交叉连接
SELECt * FROM student, classinfo;
1.等值链接
-- 查询李四所在班级,显示李四所在班级编号,姓名,班级名
-- 1.=链接两个表
SELECt student.cid,name,cname FROM student, classinfo
-- 2.确定链接条件
where student.cid=classinfo.cid
-- 3.确定其他筛选条件
and name= '李四';
-- 查询张三c++考试成绩,显示学生姓名,班级名,学号,成绩,科目名
SELECt `name`,cname,student.stuid,socre,subname
FROM student, classinfo, `subject`,score
WHERe student.cid=classinfo.cid
AND `subject`.subid=score.subid
AND student.stuid=score.stuid
AND student.`name`= '张三'
AND subname = 'c++';
2.内链接
-- 查询李四所考科目的平均成绩,显示姓名,平均成绩
-- 1.把两个表连成大表
SELECt `name`,AVG(socre) FROM student INNER JOIN score
-- 2.链接条件
ON student.stuid=score.stuid
-- 3.筛选条件
WHERe `name`='李四';
-- 查询张三c++考试成绩,显示学生姓名,班级名,学号,成绩,科目名
SELECt `name`,cname,student.stuid,socre,subname
FROM student INNER JOIN classinfo
ON student.cid=classinfo.cid
INNER JOIN score
ON student.stuid=score.stuid
INNER JOIN `subject`
ON `subject`.subid=score.subid
WHERe student.`name`= '张三' AND subname = 'c++';
3.外连接
1.左外连接:(推荐使用左外连接替代右外连接)以左表为主,在右表找匹配数据,没有就用null填充,左表数据必须被查询出来
而内连接和等值链接是左右两个表的交集,如果匹配成功则查询出来,否则不显示
-- 查询所有学生课程的考试成绩,
-- 查询结果保留学生ID、姓名、性别、课程ID、成绩
SELECt student.stuid,`name`,sex,subid,socre FROM student LEFT JOIN score
ON student.stuid=score.stuid;
2.右外连接:以右表为主,在左表找匹配数据,没有就用null填充,左表数据必须被查询出来
-- 而内连接和等值链接是左右两个表的交集,如果匹配成功则查询出来,否则不显示
SELECt student.stuid,`name`,sex,subid,socre FROM student RIGHT JOIN score
ON student.stuid=score.stuid;