本帖最后由 武汉20期-殷慧 于 2022-10-19 19:57 编辑
【对表数据的操作】
insert into 增加数据
举例:insert into user(id,name,class,phone)values(1,'xiaoshan'1001,13588888888); 在user表中插入一条数据
insert into user(name,class,phone)values('liu',1002,13511111111),('zhen',1003,13522222222); 在user表中插入多条数据
insert into user(name)values('zhou'); 给特定的字段插入一个值,主键自增长会自动生成一个不重复的id,没有非空约束的字段显示null值,有默认值约束的字段显示默认值(null为空的属性,不代表空格,也不代表0)
insert into user values(5,'wang',1003,15811111111); 在插入的数据字段数等于表结构的字段数时,可以不写字段名称
select 查询数据(对表格中的数据不会进行修改,只是把符合条件的显示出来)
select * from user; 查看表格user表中所有的数据(*代表的是通配符)
select id,class from user; 查询表中所有的id和class字段
select * from user where class = 1001; 查询user表中1001班中所有的数据
select name,phone from user where class = 1001; 查询user表中1001班中的name和phone字段(输入的目标顺序就是显示的顺序,不是表中的默认字段顺序)
select * from user where name = 'lisi' and class = 1002; 查询user表中name为lisi且class为1002的数据信息
select * from user where name = 'lisi' or class = 1002; 查询user表中name为lisi或者class为1002的数据信息
select * from user where class != 1001; 查询表中class不等于1001的数据
select * from user where id between 7 and 10; 查询表格中id在7-10之间的数据信息
select * from user where id in(7,9,10); 查询id为7,9,10的数据信息
select * from user where id not in(7,9,10); 查询id不是7,9,10的所有数据信息
select * from user where class is null; 查询class为空的数据
select * from user where class is not null; 查询class不为空的数据
select * from user where name like '%rou%'; 模糊查询包含rou的数据(可以左模糊,右模糊,中间模糊,两端模糊)
select * from user where id limit 1,4; 查询1到4行的数据(limit m,n m表示下标(索引),n表示从m开始显示的行数)
order by 排序
asc 升序
desc 降序
select * from user order by id asc; 按照id字段升序排列
select * from user order by id desc; 按照d字段降序排列
group by 分组
select * from user group by class; 按照班级分组,每组只显示一条数据
select class,count(*) from user group by class; 按照班级分组,并显示每个组的组名和人数
select *from user group by class having class = 1002 and id !=15; 查出不是1002班的并且id不是15的同学有多少人
(group by不能跟where连用,后面接条件需要用having)
聚合函数
count() 统计 select count(*) from user; 统计表中总共有多少条数据
sum() 求和 select sum(id) from user; 求出id字段的和
avg() 平均数 select avg(id) from user; 求出id字段的平均值
max() 最大数 select max(id) from user; 求出id的最大值
min() 最小数 select min(id) from user; 求出id的最小值
distinct() 去重 select distinct(phone) from user; 对表中的phone进行去重显示
update 修改数据
update user set phone =17688888888 where id = 5; 将id=5的电话修改为17688888888
update user set phone = 02788888888 where class = 1002; 将1002班的学生电话修改02788888888
delete/truncate/drop 删除数据
delete from user where id = 5; 删除id=5的数据
delete from user; 删除整张表的数据
truncate user; 快速删除整张表的数据
drop table user; 删除整张表
|
|