一.SQL查询语句
1.select from查询
语法:select 字段名 from 表名。
*表示全部
select name from student;
select name,sex from student;
select name,sex,age from student;
# 查询所有的列
select * from student;
2.where条件查询
select * from student where name = '张三'
# 查询出年龄大于20岁的同学信息
select * from student where age > 20;
# 查询出年龄不等于20岁的同学
select * from student where age <> 20;
# 查询出年龄不小于20岁的
select * from student where age != 20;
不等于有!=和<>两种方式,相同用=而不是==。
要查询的条件列的值介于两个值之间可以用between...and... []闭合区间
SELECT * from student WHERE age BETWEEN 20 and 22
in 条件列的区间是否包含在这当中,用于同时匹配多个值。
select * from student where id in(1,2,3,4)
判断字段是否为空使用is null。
# 查询class_num字段是否为空的字段
select * from student where class_num is null
涉及到多个条件使用 and 和 or。
# 查询出20201001班的男同学和其他班的女同学
select * from student where class_num = '20201001' and sex = '男'
select * from student where sex = '女' and class_num != '20201001' or class_num = '20201001' and sex = '男'
模糊查询:根据关键词进行搜索。使用like,通配符 % 可以匹配任意多个字符,_ 只能配置一个字符。
select * from student where name like '王%'
3.聚合函数
常用的有max(),min(),count(),avg()这四个函数,写在查询字段的位置,括号里面写字段。
# 查询学生中的最大年龄
select max(age) from student;
# 查询学生中的最小年龄
select min(age) from student;
# 查询学生的平均年龄
select avg(age) from student;
# 查询表当中有多少记录(多少条数据)
select count(*) from student;
# 1. count(*) 表示取得当前查询表所有记录
# 2. count(字段名称), 不会统计为null的记录
select count(class_num) from student;
4.group by分组查询
group by用于根据某一个字段进行分组。
# 查询出各个班级的平均年龄
select avg(age) avg_age,class_num from student GROUP BY class_num;
# 查询出各个班级男女同学的数量
select count(*) sex_count,sex,class_num from student GROUP BY class_num,se
5.order by排序
ASC指升序(默认),DESC指降序。
# 根据age字段从小到大进行排序
select * from student ORDER BY age
# 根据age字段从大到小排序-------> 倒序 DESC;
select * from student ORDER BY age desc;
select * from student ORDER BY age DESC;
(SQL语句不区分大小写)
6.limit分页查询
# pageStart 从哪一条数据开始查询 0
# pageSize 每页查询的条数
# 假设每页显示5条数据
select * from student limit 5 offset 0; # 第一页
select * from student limit 5 offset 5; # 第二页
select * from student limit 5 offset 10; # 第三页
select * from student limit 5 offset 15; # 第四页
# 另一种写法 limit pageStart,pageSize
select * from student limit 0,5; # 第一页
select * from student limit 5,5; # 第二页
select * from student limit 10,5; # 第三页
select * from student limit 15,5; # 第四页
7.join多表查询
有三种连接方式:内连接(默认),左连接,右连接。
select 字段(必须标明是来自于哪个表当中的) from A表名称 join B表 on 表的链接条件
select s.*,c.class_name from student s join class c on s.class_num = c.class_num
# 连接方式
# 1.内连接 inner join
select s.*,c.class_name from student s inner join class c on s.class_num = c.class_num ; # 查询到的两个表当中相交的部分
# 2.左(右)外连接 left(right) join 显示左(右)表当中的全部信息
select s.*,c.class_name from student s left join class c on s.class_num = c.class_num ;
二.SQL增删改语句
1.insert添加数据
insert into 表名(列1,列2,列3......)value('值1','值2','值3......')
多条:insert into 表名(列1,列2,列3......)values('值1','值2','值3......'),('值1','值2','值3......'),('值1','值2','值3......')
insert into student(name,sex,age,sno,class_num) values ('张三','男',18,'1001','20201004');
insert into student(name,sex,age,sno,class_num) values ('张三','男',18,'1001','20201004'),('张三','男',18,'1001','20201004'),('张三','男',18,'1001','20201004');
设置了自动递增的字段不能添加。
2.update数据修改
update 表名 set 字段 where 条件
where必不可少!
update student set name = "双手合十",age=22,sex='女' where id=2;
3.数据删除
第一种:delete
delete from 表名 where 条件
delete from student where id =1;
第二种:truncate
truncate table 表名
把表清空但是保留原本的结构。
truncate table class;
第三种:drop
drop table 表名
彻底把这张表删除。
drop table relationship;
7181

被折叠的 条评论
为什么被折叠?



