找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
对表数据的操作
插入数据-insert into
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),('xiaozheng',1002,18899990000);    :在use表中插入多条数据

insert into user(name)values('xiao');   :在特定的字段插入数据,主键自增长会自动生成一个不重复的id,没有非空约束的字段显示null值,有默认值约束的字段显示默认值

insert into user  values(12,'zhurourong',1003,13588886666);  :在插入的数据字段数等于表结构的字段时,可以不写字段名称

insert into user(id,name,class,phone)values(7,'zhangsan',1003,13588881111);   :插入id为7的一行数据

id的自增长是以当前id最大数进行自增长,比如id4后面接的是id7,后续插入数据时未写id字段,则从id7后进行自增长到id8;id9后面接的是id5,后续插入数据时未写id字段,则从id9后进行自增长到id10,id11....(有问题)

数据的插入是如果前面有删除的行,则优先填补空缺,否则按插入顺序依次排序,例如user表中,在id9后面添加id5,也是排在id9的后面  

null 为空的属性,不代表空格,也不带0
**查询数据-select**(重点)
查询数据的时候对原表的数据是不会修改的,只会显示需要查询的数据
select * from user;  :查看user表中的所有数据 ,*号代表通配符

select id,class from user;  :显示user表中的id与class字段,多字段用逗号隔开

select * from user where class = 1001;  :查询1001班的所有数据

select name,phone from user where class = 1001;   :查询1001班的姓名和电话,查询的目标字段用逗号隔开

select * from user where class = 1002 and id = 12;   :查询班级为1002班,id序号为3的数据

查询的目标字段需要用逗号隔开,输入的目标字段顺序就是显示的顺序,不是表中的默认字段顺序

select * from user where class != 1002;   :查询表中班级不等于1002的数据

修改数据-update
update user set phone = 17621599999 where id = 5;   :将表中id=5的数据电话修改为17621599999

update user set phone = 02788888888 where class = 1002;   :将表中1002班的学生电话修改为02788888888

select * from user where name = 'lisi'  and class = 1003;  :查询姓名为lisi,且班级为1003的数据

select * from user where name = 'lisi' or  class = 1003;   :查询姓名为lisi,或班级为1003的数据

select * from user where id between 7 and 10;    :查询一个条件范围内的数据,查询id在7到10之间的数据

select * from user where id in(7,9,10);    :查询指定集合内的数据,查询集合(7,9,10)的数据

select * from user where id not  in(7,9,10);     :查询指定集合内的数据,查询id不在集合(7,9,10)的数据

select * from user where class is null;  :查询user表中class列为空值的数据
select * from user where class is not null;   :查询user表中class列不为空值的数据
select * from user where name like '%rou%'   :模糊查询,模糊部分用%代替,分左模糊,右模糊,两端模糊,中间模糊



select * from user where id limit 1,4;    :查询表中2到5行的数据,1代表的是下标,4代表行数


select * from user where id limit 0,5;   :查询user表中前5行数据,要从第一行开始显示就要从0开始写

总结

删除数据-delete/truncate/drop
delete from user where class = 1001;   :删除1001班的数据
delete from user where id = 5;    :删除id=5的数据
delete from user where class is null  :删除班级数据为空的数据
delete from user;   :删除整张表的数据
truncate user;    :快速清空整张表
drop table user    :删除整张表
排序-order by
升序-asc
select * from user order by id asc;  :按id字段升序排列,表内数据没有改动

降序-desc
select * from user order by id desc;   :按id字段降序排列,表内数据没有改动

分组-group by  
不能与where连用,后面接条件需要用having
select * from user group by class;  :按照班级分组,不会显示全部数据,主要来查看有多少个组以及组名

select class,count(*) from usetr 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;  :计算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表中的电话进行去重显示

补充
1、where 不能放在GROUP BY 后面,where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数比如SUM(),AVG()等,使用where条件显示特定的行。
2.having 是跟GROUP BY 连在一起用的,放在GROUP BY 后面,此时的作用相当于WHERE。 having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。

分享至 : QQ空间
收藏

0 个回复

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