找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
10.19笔记
【对表数据的操作】
【增加数据--insert into】
insert into tbuser(id,name,class,phone)values(1,'xiaoshan',1001,13588888888);:在user表中插入一条数据


insert into user (name,class,phone)values('xiaoliu',1001,1358889999),('xiaozheng',1002,188899990000);:在user表中插入多条数据

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

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

null为空的属性,不代表空格,也不代表0

【查询--select】,查询数据的时候对原表的数据是不会修改的,只是把符合条件的显示出来
select * from user;:查看user表中的所有数据,*代表的是通配符

select id,class from 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 and id=3;:查询1002班id为3的所有数据
select name,phone from user where class = 1002 and id=3;:查询1002班id为3的姓名和电话
select * from user where class !=1002;:查看表中不等于1002的数据
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 9;:查询id在7到9之间的数据

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

select * from user where id not in(7,9,10);:查询id不是7,9,10的数据

select * from user where class is null;:展示user表中班级是空的数据
select * from user where class is not null;:展示user表中班级不是空的数据
select * from user where name like '%rong%';:模糊查询数据,模糊的部分用%代替

select * from user where name like 'zhu%';:右模糊匹配
select * from user where name like '%ron';:左模糊匹配

select * from user where name like '%zhu%';:两端模糊
select * from user where name like 'zhu%ron';:中间模糊

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

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

【修改数据--update】
update user set phone = 176458003232 where id = 5;:将表中id = 5的数据的电话修改为176.....

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

【删除数据-delete/truncate/drop】
delete from user where class = 1001;:删除1001班的数据
delete from user where id = 5;:删除id=5的数据
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
(group by 不能跟where连用,后面接条件需要用having)
select * from user group by class;:通过班级分组

select class,count(*) from user group by class;:通过班级分组,并显示组名和每个组的人数

select * from user group by id    having class=1002 and id !=15; :查出姓名为1002的班级且id不是15的同学


【聚合函数】
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表中的电话进行去重显示
分享至 : QQ空间
收藏

0 个回复

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