SQL语句分类:
名词 | 解释 | 命令 |
DDL(数据定义语言) | 定义和管理数据对象,如数据库、表等 | create、drop、alter |
DML(数据操作语言) | 用于操作数据库对象中所包含的数据 | insert、update、delete |
DQL(数据查询语言) | 用于查询数据库数据 | select |
DCL(数据控制语言) | 用来管理数据库语言,包括管理权限及数据更改 | commit、rollback |
DML语句:
新增:insert into 表名(字段名...字段名) values (值...值)
全字段插入(例子):
1:insert into student(sid,sname,birthday,ssex,classid)
values(9,'张三','2000-1-1','男',10);
2:insert into student values(10,'李四','2001-2-2','女',12);
修改(必须要加条件):update 表名 set 字段名=值,字段名=值,...,字段名=值
where 子句 【条件】
多条件的修改 要用连接符号 and 或者 or:
例子: update stu1 set birthday='2022-8-26' where classid = 1 and sname = '赵雷'
范围型的多条件:
例子:update stu1 set birthday='2022-8-26' where classid >= 4 and classid <= 10
between 闭合区间 较小的数据(向右) and 较大的数据(向左):
例子:update stu1 set sname = '无名1' where classid between 4 and 10
删除(必须带条件):delete from 表名 where 子句【条件】
带条件的删除:
例子:delete from stu1 where sname = '无名' and classid = 7
DQL语句:
全字段查询(sql优化 不要用 * 号代替字段名):
例子:select sid,sname,birthday,ssex,classid from student;
起别名:
例子:select sname as '学生姓名',sname '姓名',sname 名字 from student;
去除重复的数据 (distinct 后面的字段的值要完全相同时才会去除重复):
例子:select distinct sid, sname,ssex from student;
like 模糊查询:
1. 模糊符号 % 任意多的任意字符
2. 模糊符号 _ 一个任意字符
前模糊 %key 后模糊 key% 前后都模糊 %key%
select * from student where sname like '%薛%';
姓薛 单名一字
select * from student where sname like '薛_';
in 在某个特定范围内:
例子:select * from student where sid in (1,2,5,8,13,200,3000,40000)
查询 null 值:
select * from student where classid = null; 大错特错
例子1:select * from student where classid is null;
例子2:select * from student where birthday is not null;
having :
where 后的筛选是针对表中的每一条数据进行
having 筛选 分组之后的数据进行筛选 having 不能单独出现必须要有group by
例子:每个学生总成绩( 总成绩 大于200分,只统计 成绩高于60分)
select sid, sum(score) from sc where score > 60 group by sid having sum(score) > 200
order by : 多个字段进行排序 (先写先排)
升序、降序
例子1:select * from student order by sid asc -- 默认规则升序
例子2:select * from student order by sid desc -- 降序
limit 分页 : limit 数字【获取多少个】 limit 数字 【位置】, 数字【步长】
分页公式 (页码-1)*步长, 步长
例子:select * from student limit (1-1)*3,3
外联查询 -- 主表 从表
左外联: left join on
例子: 所有的学生信息和对应的班级信息
select * from student left join class on student.classid = class.classid
右外联:right join on
select * from class right join student on student.classid = class.classid
內联查询: inner join on
例子:统计每个老师所带的学生平均分是多少? 老师信息和平均成绩
select tname, avg(score) from teacher
inner join course on teacher.tid = course.tid
inner join sc on course.cid = sc.cid
group by teacher.tid order by avg(score) desc
where 子查询:所有的子查询都需要用小括号括起来
例子:select * from student where sid = (select max(sid) from student)
from 子查询 :要给虚拟表起一个表名
例子:select * from class left join (select classid,count(*) 人数 from student group by classid) t on class.classid = t.classid where 人数 > 5
exists 子查询 :子句有数据 父句执行 ,子句没有数据 父句不执行
例子: 如果学生表中有男同学则查询出所有的老师
select * from teacher where exists (select * from student where ssex = '非男';
any some 子查询
some --> any
例子:查询出一班成绩比二班最低成绩高的学生
select * from student inner join sc on student.sid = sc.sid
where classid = 1 and score > (select min(score) from student inner join sc on student.sid = sc.sid where classid = 2)
IF(expr1,expr2,expr3):
expr1 条件
expr2 当条件成立的值
expr3 当条件不成立的值
例子:select tid,tname, if(tsex=1,'男','女') tsex,tbirthday from teacher
IFNULL(expr1, expr3):
expr1 字段
expr2 默认值
例子:select sid,sname,ifnull(birthday,'这个学生没有生日') bir, ssex,classid from student
case when then end :
1. 简单case
例子:select tid,tname,
case tsex
when 1 then '男'
when 0 then '女'
end tsex,tbirthday,taddress,temail,tmoney from teacher
2.搜索case
例子:select tid,tname,
case
when tsex > 0 then '男'
when tsex = 0 then '女'
when tsex < 0 then '未知'
else 'xx' end, tbirthday,taddress,temail,tmoney from teacher