目录
准备
stu 类
一,数据相关
1,增加数据
一,给所有字段赋值
insert into 表名 (字段1,字段2...) values (值1,值2...);
insert into 表名 values (值1,值2...);
二,给部分字段赋值
insert into 表名 (字段1,字段2) values (值1,值2);
2,修改数据
update 表名 set 字段1=值1,字段2=值2...where 字段=值;
注:where后面的条件一般都使用唯一的那个
3,删除数据
delete from 表名 where 字段=值;
4,查询表中数据
select 字段名1,字段名2... from 表名; [查询所有列值,也可以用*代替]
注:出现乱码运行set names gbk;
5,带条件的查询
select 字段 from 表名 where 字段=值;
注:select后面 from前面的字段就是显示给用户看的内容。
6,排序 asc [默认,升序] / desc [降序]
select * from 表名 where 字段=值 order by 字段名 desc/asc;
7,查询在xx 到xx之间的数据
select * from 表名 where 字段 between xx and xx;
8,查询满足条件的数据
select * from 表名 where 字段 >条件1 and 字段<条件2;
注:and 表示并列 (与) or (或) not(非)
9,in查询
select * from 表名 where 字段 in(值1,值2...);
10,模糊查询
select 字段 from 表名 where 字段 like '%xx%';
# like '%xx%'; --->表示包含xx的内容
# like '__x%'; --->表示第二个位置是x的内容
# like '%x'; ---> 表示以x结尾的内容
# like 'x%' --->表示以x开头的内容
11,查询某列为空/不为空的数据
select * from 表名 where 字段 is null;
select * from 表名 where 字段 is not null;
12,分页
select * from 表名 limit index,pageSize;
# index 表示数据的索引位置 从0开始
# pagaSize 表示每页显示的条数
13,别名
select 字段 as 别名 from 表名;
注:as 可以省略
14,聚合函数
就是系统给我们提供的一些函数,可以直接使用
max() 获取最大值
min()获取最小是
avg()获取平均值
count()获取有多少条数据
sum()计算和
concat(a,b); 将a b 拼接到一起
注:中文拼接有乱码
15,子查询
要求:找到成绩最高的人的姓名
① 先找到最高成绩 select max(score) from stu; --->99.0
② 找出成绩是99的人的姓名 select name from stu where score=99; -->lisi
二合一: 将等号后面括号里面的sql称之为子句, #分析的时候 先分析字句,写的时候先写父句 select name from stu where score=(select max(score) from stu);
16,分组函数
select 聚合函数 ,字段1 from 表名 group by 字段1;
注意:除了聚合函数之外在select后面出现的字段都需要写在group by 后面
17,对分组之后的结果进行筛选
select 聚合函数 ,字段1 from 表名 group by 字段1 having 聚合函数>条件1;
注:having是对分组之后的结果进行筛选,where是对分组之前的结果进行筛选