找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
对表数据的操作
增加数据---insert into
insert into user(id,name,class,phone)values(1,'xiaoshan',1001,13588888888);:在user表中添加一条数据
insert into user(name,class,phone)values('xiaoliu',1001,13588889999),('xiaowang',1002,13588887777);:在user表中插入多条数据
insert into user(name)values('xiao');:在user表中特定的字段添加数据,主键自增长会自动生成一个不重复的id,没有非空约束的字段显示null值,有默认值约束的字段显示默认值
NULL 为空的属性,不代表空格,也不代表0
insert into user values(23,'zhurourong',1003,15851851851);:在插入数据的字段数等于表结构的字段数的时候是可以不写字段名称的

查询数据--select 查询数据的时候对原表数据不会修改,只是把符合条件的数据显示出来
select * from user;:查询user表中的所有数据,*代表通配符
select id,class from user;:查询user表中的id,class字段的所有数据
查询出user表中的1001班的所有数据
select * from user where class=1001;
select name,phone from user where class=1001;:查询1001班所有人的姓名和电话,查询的目标字段中间用逗号隔开,目标字段的顺序就是显示的顺序,不是原表中的默认字段的顺序
select * from user where class != 1002;:查询user表中class字段不是1002的数据
select * from user where name='lisi' and class=1003;:查询user表中name字段为lisi且class字段为1003的数据
select * from user where name='lisi' or class=1003;:查询user表中name字段为lisi或者class字段为1003的数据(只需满足其中一个条件就可以显示出来)
select * from user where id between 7 and 11;:查询user表中id字段在7-11之间的数据(包含7和11)
select * from user where id in(7,9,10);:查询user表中id字段在7,9,10之内的数据
select * from user where id not in(7,9,10);:查询user表中id字段除了7,9,10之外的数据
select * from user where class is null;:查询user表中class字段为null的数据
select * from user where class is not null;:查询user表中class字段不为null的数据
select * from user where name like '%rou%';:查询user表中name字段中间包含rou的数据(模糊查询),%代表模糊的内容
select * from user where id limit 0,5;:查询user表中1-5行的数据(0是开始的行的下标,5代表显示的行数),当从首行开始时,0可以省略

修改数据--update
update user set phone=17621599999 where id=5;:将user表中id字段为5的数据的电话号码改为17621599999
update user set phone=02788888888 where class=1002;:将user表中class字段为1002的数据的电话号码统一修改为02788888888
update user set name='yu',class=1004 where id=20;:将user表中id字段为20的数据的姓名和班级分别改为yu和1004,更改的字段中间用逗号隔开

删除数据--delete/truncate/drop
delete:删除指定条件的数据
delete from user where id = 5;:删除user表中的id字段为5的数据
delete from user;:删除user表中所有的数据
truncate:快速清除表里的所有数据
truncate user;:快速清空user表中的所有数据
drop:删除表结构
drop table user;:删除user表

排序--order by
升序:asc
select * from user order by id asc;:将user表中的数据按照id字段升序排列,不会对原表数据进行排序
降序:desc
select * from user order by id desc;:将user表中的数据按照id字段降序排列,不会对原表数据进行排序

分组--group by
select * from user group by class;:将user表中的数据通过class字段分组
select class,count(*) from user group by class;:将user表中的数据通过class字段进行分组并统计每个组的人数
select * from user group by id having id !=15 and class =1002;:查出user表中通过id字段分组,class字段为1002且id字段不为15的数据
select class,avg(id) from user group by class having avg(id)>11;:查出user表中通过class字段分组,平均id大于11的数据

聚合函数
count():统计
select count(*) from user;:查看user表中有多少条数据
sum():求和
select sum(id) from user;:查看user表中所有id的和
avg():求平均数
select avg(id) from user;:查看user表中所有id的平均值
max():求最大值
select max(id) from user;:查看user表中的最大id
min():求最小值
select min(id) from user;:查看user表中的最小id
distinct():去重
select distinct(phone) from user;:对user表中的phone字段的数据进行去重显示

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册