-- 查询所有的学生信息 select select 字段 from 表
select name from student;
select name,sex,age from student;
-- 查询所有的列
select * from student;
-- 条件查询
-- where 条件列 运算符 值
select * from student WHERE name="zhangsan"
-- 查询出年龄大于20岁的同学信息
SELECT * FROM student WHERE age > 20;
-- 查询出年龄不等于20岁的同学信息
SELECT * FROM student WHERE age <> 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)
select * FROM student WHERE name in('zhangsan','李四','王五')
-- 判断字段是否为空,is NULL
select * from student where reteTime is NULL
-- 涉及到多个条件怎么办?
-- 查询出20201001班的男同学 和 and 或 or
select * from student where class_num = '20201001' and sex= '男'
-- 查询出20201001班的男同学或女同学
select * from student where class_num = '20201001' and sex= '男' or sex ='女' and class_num != '20201001'
-- 模糊查询,根据关键词进行搜索
-- 查询出学生中有王的同学
SELECT * FROM student where name like '%王%'
-- 通配符 % 可以匹配任意多个字符 _只能匹配一个字符
-- 分组聚合查询
-- 常见聚合函数,
-- 查询出来的是一个值
-- sum()求和
-- avg() 取平均数
-- max() 取最大值
-- min() 取最小值
-- count() 去记录数
-- 查询出同学们当中年龄最大的
select max(age) from student;
select avg(age) from student;
SELECT count(*) from student;#查询表当中有多少记录(多少条数据)
-- 注意:1.count(*)表示取得当前查询表所有记录,2.count(字段名称),不会统计为null的记录
-- 分组 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,sex
-- 查询排序,一般用于数据的展示
select * from student #默认的数据排序规则:根据id从小到大进行排序
-- 新插入的数据id是大的,新插入的数据往往再末尾展示
-- 想要新插入的数据在前面展示
-- order by 字段
#根据age字段从小到大排序
select * from student order by age
#根据age字段从大到小排序 倒序 desc
select * from student order by age desc;
-- 分页查询(限制查询)减小系统压力
-- limit pageSize offset pageStart
-- pageSize 每页查询的条数
-- pageStart 从那一条数据源开始查询
-- 假设每页显示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 ;#第四页
-- 注意:一般情况下:前端给我们提供的参数 pageSize(每页显示数据量) pageIndex(页码,1 2 3 4 5 6)
-- SELECT * FROM student limit pageSize offset (pageIndex-1)* pageSize;
-- 链表查询 也叫跨表查询,涉及到多张表查询
-- 查询学生的班级的名称
-- 笛卡尔积现象:若两张表进行连接查询的时候没有任何条件限制,最终的查询结果总数是两张表记录的成绩,该现象称为笛卡尔积现象。
select s.name c.class_name from student s,class c
-- SQL 92语法 :select xxx from a 表名, b 表明 where 表连接条件 and 数据查询条件;
select s.name,c.class_name from student s,class c where s.class_num = c.class_num
-- 提前把where给占据掉了,链接条件和查询条件被放在一起了,
-- SQL 99语法:select 字段(必须标明是来自于哪个表当中) from A表名称 join B表 on 表的链接条件
select s.name,c.class_num from student s join class c on s.class_num = c.class_num
-- 连接方式
-- 1.内连接
select s.*,c.class_num from student s inner join class c on s.class_num = c.class_num;#查询到的是两个表当中相交的部分
-- 2.左外连接 left join 显示左表当中的全部信息
select s.*,c.class_name from student s left join class c on s.class_num = c.class_num;
-- 2.右外连接 right join 显示左表当中的全部信息
select s.*,c.class_name from student s right j oin class c on s.class_num = c.class_num;
-- 嵌套查询 增删改 1.操作 2.力扣
-- sql量非常大——————》最简单
(select name ,sex,age from student) t1 where t1.name = '张三'
-- 查询每一个选择语文课长的同学的信息。
-- 查询出每一个同学所选择的课程号 t1
select t1.*,course.gradeName from
(select student.*,relationship.cno from student left join relationship on student.sno = relationship.sno) t1 left join course on t1.cno = course.cno where course.gradeName = '语文' and t1.age > 21;
-- #1.查询选择语文课程的,且年龄超过21岁的同学信息
select t1.*,course.gradeName from
(select student.*,relationship.cno from student left join relationship on student.sno = relationship.sno) t1 left join course on t1.cno = course.cno where course.gradeName = '语文' and t1.age > 21;
-- 2.查询出软件工程班年龄最大的同学所选择的是哪些课程
-- 倒序+限制查询
select * from student order by age desc limit 1 offset 0
select t2.*,course.gradeName from
(select t1.*,relationship.cno from
(select student.*,class.class_name from student left join class on student.class_num = class.class_num order by student.age desc limit 1 offset 0)t1
left join relationship on relationshipl.sno = t1.sno)t2 left join course on course.cno=t2.cno;
-- select t2.* from
-- (select t1.*,course.gradeName from
-- (select student.*,relationship.cno from student left join relationship on student.sno = relationship.sno) t1 left join course on t1.cno = course.cno) t2
-- order by t2.age desc limint 1 offset 0;
-- 数据添加
-- 单行添加方式
-- insert into 表名(列1,列2 ,列3 .。。。)value(’值1‘,'值2','值3'....)
#多行添加方式
-- insert into 表名(列1,列2 ,列3 .。。。)value(’值1‘,'值2','值3'....),(’值1‘,'值2','值3'....)
-- 插入注意事项:插入的时候不能插入id,因为id设置了自动递增。
-- 2.插入数据的时候varChar类型的数据需要加一个单引号,数值型数据不需要
-- update修改操作
#update 表名 set 列名 = 新值,列名 = 新值 where 条件列 = 值 ;
-- 注意关于条件列where 条件列 = 值
-- delete 删除操作,第一种删除方式
-- delete from 表名 where 条件列 = 值;
DELETE from student where name = 'zhangsan'
delete form student
-- truncate table 表名,第二种删除方式
TRUNCATE table class
-- delete 可以一条一条的删除数据,效率低下 后面可以跟where条件
-- truncate 是全部清空再创建一张新表
-- drop table 表名 彻底删除这张表
drop table relationship;