-- where 条件是整张表每一条数据进行判断 ****
update stu1 set xingbie ='女' where xingming='王五';
where 针对表中每一行数据
sql复制代码
-- where 条件是整张表每一条数据进行判断 ****
update stu1 set xingbie ='女' where xingming='王五';
-- 范围形
-- 方式一
-- 闭合区间
update stu1 set classid =100
where stuid >=10 and stuid <=15;
sql复制代码
-- 方式二: -- between 跟较小的数据 and 较大的数据
update stu1 set classid =300
where stuid between 10 and 15;
3. 删除数据(DELETE语句)
语法:
sql复制代码
DELETE FROM 表名 [ WHERE 条件];
其中:
•"[]" 包含的内容可以省略;
• "WHERE"可以通过where子句增加删除的条件。
sql复制代码
-- 删除
-- delete from 表名 【where 子句】
delete from stu1
insert into stu1(xingming) values('张三')
-- 计算列
CREATE TABLE jsltab(
id INT PRIMARY KEY AUTO_INCREMENT,
a INT ,
b INT ,
c INT GENERATED ALWAYS AS (a + b) VIRTUAL
);
select * from jsltab
insert into jsltab(a,b) values(2,3),(200,300)
SELECT * FROM 表名 [ WHERE 条件];
其中:
• WHERE条件子句不是必须的;
• WHERE子句,可以给查询增加条件;
• 条件:为筛选条件,如不指定则修改该表的所有数据。
sql复制代码
-- 带条件的查询 where子句
select * from student
where Ssex ='男' and classid =1;
练习:
sql复制代码
-- 查询Sid 2-6 的学生
select * from student
where Sid >=2 and Sid <=6;
select * from student
where Sid between 2 and 6;
sql复制代码
-- 查询出年龄大于1990-1-1的学生
select * from student
where birthday < '1990-1-1';
LIKE 关键字:
语法:
sql复制代码
SELECT * FROM 表名 WHERE 字段 LIKE 条件;
其中:
• 在WHERE子句中,使用LIKE关键字进行模糊查询;
• 与"%"一起使用,表示匹配0或任意多个字符;
• 与"_"一起使用,表示匹配单个字符。
sql复制代码
-- like 模糊查询
insert into student (sname) values
('张老师'),('小张'),('张美女'),('姓张的老师');
-- % 任意多的任意字符
-- _ 一个任意字符
select * from student where Sname like '%张%';
select * from student where Sname like '张%';
select * from student where Sname like '%张_';
select * from student where Sname like '%张__';
IN 关键字:
语法:
sql复制代码
SELECT * FROM 表名 WHERE 字段 IN (值1,值2...)
其中:
• 查询的字段的值,至少与IN 后的括号中的一个值相同;
• 多个值之间用英文逗号隔开。
sql复制代码
-- in 在特定的范围内
-- 1,3,4,5,8
select * from student where Sid =1 or Sid =3 or Sid =4 or Sid =5 or Sid =8;
select * from student where Sid in (1,3,4,5,8);
NULL 值查询:
语法:
sql复制代码
SELECT * FROM 表名 WHERE 字段 IS NULL | IS NOT NULL
其中:
• NULL代表"无值";
• 区别于零值0和空符串;
• 只能出现在定义允许为NULL的字段;
• 须使用 IS NULL 或 IS NOT NULL 比较操作符去比较。
sql复制代码
-- 对 null 的操作
select * from student where birthday is null;
select * from student where birthday is not null;
ORDER BY排序
语法:
sql复制代码
SELECT * FROM 表名 ORDER BY 字段名 [DESC|ASC]
其中:
• ORDER BY 表示对SELECT语句查询得到的结果,按字段名进行排序;
• DESC表示排序的顺序为降序,ASC表示排序的顺序为升序;
•"[ ]"包含的内容可以省略。
sql复制代码
-- 排序
-- asc (或不写)就是升序
-- desc 降序
-- 先写先排
select * from student order by classid desc, birthday asc;
LIMIT关键字:
语法:
sql复制代码
SELECT * FROM 表名 LIMIT [n , m ]
其中:
• LIMIT关键字是MySQL特有关键字;
• LIMIT限制SELECT返回结果的行数;
• n 表示第一条记录的偏移量,m 表示显示记录的数量;
•"[ ]"包含的内容可以省略。
sql复制代码
-- 分页 limit 位置,步长 如果不写位置 默认为0
select * from student limit 0,3;
select * from student limit 3,3;
select * from student limit 6,3;
select * from student limit (页码-1)*步长,步长;
select * from student limit (2-1)*3,3;
LIMIT实现分页显示的方式:
MySql8新关键词OFFSET:
语法:
sql复制代码
SELECT * FROM 表名 limit m offset n
其中:
• LIMIT关键字是MySQL特有关键字;
• LIMIT限制SELECT返回结果的行数;
• n 表示第一条记录的偏移量,m 表示显示记录的数量;
•"[ ]"包含的内容可以省略。
-- ***** 聚合函数
-- count 个数 --全类型
-- count 不统计null
-- 学生表中一共有多少个人
select count(*) from student;
select count('abc') from student; -- 常量
select count(Sid) from student;
select count(birthday) from student;-- null 不会被count计算到
sql复制代码
-- 得到男同学的人数
select count(*) from student where Ssex='男';
sum
sql复制代码
-- sum 总和
select sum(score) from sc;
练习:
sql复制代码
-- max 最大值
-- min 最小值
-- avg 平均值
-- 统计出sc表
-- 一共的考试次数 ,总成绩,平均分, 最高分,最低分
-- sum 总和
select count(*) 考试次数,sum(score) 总成绩,avg(score) 平均分,max(score) 最高分,min(score) 最低分 from sc;
3.1 GROUP BY
对所有的数据进行分组统计;
分组的依据字段可以有多个,并依次分组。
sql复制代码
-- 查询出男同学和女同学各有多少个人
select count(*) from student where Ssex ='男';
select count(*) from student where Ssex ='女';
-- 上面两句等同于这句
select Ssex,count(*) from student group by Ssex;
sql复制代码
select Ssex,max(Sid) from student group by Ssex;
练习:
sql复制代码
-- 查出每个学生的平均分
select Sid,avg(score),count(*) from sc group by Sid;
3.2 HAVING
与GROUP BY结合使用,进行分组后的数据筛选。
sql复制代码
-- 找到平均分不及格的学生
select Sid,avg(score)from sc group by Sid having avg(score)<60;
如果是 SQL Server 和 Access,需要使用 TOP 关键字,比如:
SELECT TOP 5 name,hp_max FROM heros ORDER BY hp_max DESC
如果是 DB2,使用 FETCH FIRST 5 ROWS ONLY 这样的关键字:
SELECT name, hp_max FROM herOS ORDER BY hp_maX DESC FETCH FIRST 5 ROWS ONLY
重要:
如果是 Oracle,你需要基于 ROWNUM 来统计行数:
SELECT rownum,last_name,salary FROM employees WHERE rownum < 5 ORDER BY salary DESC;
四、总结
DML语句内容?
INSERT语句,UPDATE语句和DELETE语句;
新增语句如何实现多记录同时新增?
INSERT INTO `表名` (`字段1`,`字段n`) VALUES (值1,值n),(值1,值n),(值1,值n);