【对表数据的操作】 【增加数据--insert into】 insert into user(id,name,class,phone)values(1,'xiaoshan',1001,13222222222); :在user表中插入一条数据 insert into user(name,class,phone)values('xiaowang',1001,13244444444),('xiaozeng',1001,13255555555); :在user表中插入多条数据 insert into user(name)values(xiaoling); :在特定的字段插入数据,主键自增长会自动生成一个不重复的id,没有非空约束的字段显示NULL值,有默认值约束的字段显示默认值 insert into user values(12,'zhurourong',1003,158518518518); :在插入的数据的字段等于表结构的字段数的情况下,可以不写字段名称的 null为空的属性,不代表空格,也不代表0 【查询数据 --select】查询数据的时候对原表的数据是不会修改的,只是把符合条件的显示出来 select * from user; :查看user表中的所以数据,* 代表的是通配符 select id,class from user; :查询user表中的id和class字段 select * from user where class=1001; =》查询出user表中1001班的所有数据 select name,phone from user where class=1001; =》查询1001班的姓名和电话,查询的目标字段需要用逗号隔开,输入的目标字段的顺序是显示的顺序,不是创建的字段 select * from user where class!=1002; :查询表中class不等于1002的数据 select * from user where name='xiaoling' and class=1002; :查询姓名是xiaoling,且班级是1002班的数据 select * from user where name='xiaoling' or class=1002; :查询姓名是xiaoling,或者班级为1002班的数据 select * from user where id between 2 and 6; :查询id在2到6之间的数据(闭区间) select * from user where id in(3,8); :查询id在这个集合内的数据 select * from user where id not in(3,8); :查询id不在这个集合内的数据 select * from user where name like "%qi%"; :模糊查询数据,通过%去匹配模糊的数据 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=17621595959 where id=7; :将表中id=5的数据的电话修改为17621595959 update user set class=1002 where id=7; :将表中id=7的数据的班级修改为1002 update user set phone=02788887777 where class=1001; :将表中班级为1001的数据的电话改为02788887777 【删除数据--delete/truncate/drop】 delete from user where class=1001; :删除1001班的数据 delete from user where id=7; :删除id为7的数据 【对表数据的操作】 【增加数据--insert into】 insert into user(id,name,class,phone)values(1,'xiaoshan',1001,13222222222); :在user表中插入一条数据 insert into user(name,class,phone)values('xiaowang',1001,13244444444),('xiaozeng',1001,13255555555); :在user表中插入多条数据 insert into user(name)values(xiaoling); :在特定的字段插入数据,主键自增长会自动生成一个不重复的id,没有非空约束的字段显示NULL值,有默认值约束的字段显示默认值 insert into user values(12,'zhurourong',1003,158518518518); :在插入的数据的字段等于表结构的字段数的情况下,可以不写字段名称的 null为空的属性,不代表空格,也不代表0 【查询数据 --select】查询数据的时候对原表的数据是不会修改的,只是把符合条件的显示出来 select * from user; :查看user表中的所以数据,* 代表的是通配符 select id,class from user; :查询user表中的id和class字段 select * from user where class=1001; =》查询出user表中1001班的所有数据 select name,phone from user where class=1001; =》查询1001班的姓名和电话,查询的目标字段需要用逗号隔开,输入的目标字段的顺序是显示的顺序,不是创建的字段 select * from user where class!=1002; :查询表中class不等于1002的数据 select * from user where name='xiaoling' and class=1002; :查询姓名是xiaoling,且班级是1002班的数据 select * from user where name='xiaoling' or class=1002; :查询姓名是xiaoling,或者班级为1002班的数据 select * from user where id between 2 and 6; :查询id在2到6之间的数据(闭区间) select * from user where id in(3,8); :查询id在这个集合内的数据 select * from user where id not in(3,8); :查询id不在这个集合内的数据 select * from user where name like "%qi%"; :模糊查询数据,通过%去匹配模糊的数据 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=17621595959 where id=7; :将表中id=5的数据的电话修改为17621595959 update user set class=1002 where id=7; :将表中id=7的数据的班级修改为1002 update user set phone=02788887777 where class=1001; :将表中班级为1001的数据的电话改为02788887777 【删除数据--delete/truncate/drop】 delete from user where class=1001; :删除1001班的数据 delete from user where id=7; :删除id为7的数据 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 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; :统计user表中有多少条数据 sum():求和 select sum(id) from user; :求出所有id的字段和 avg():平均数 select avg(id) from 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表中的电话进行出重显示
|