mysql命令
第十一天学习笔记mysql命令
查找表里的字段:select+字段from +表名;
查找表里的多个字段:select 字段,字段from表名;
查询满足某个条件的字段:select* from表名 where字段=值;
查询满足多个条件的字段:select* from表名 where class=1833 or class=1844;
查询不满足某个条件: select* from表名 where class!=1833;
或select* from表名 where class<>1833;
查询两个字段满足某个条件:select*from表名 wherename=‘zhangsan’ andclass=1833;
查询某个字段满足某个区间:select*from 表名 whereage between 25and 31;
或 select*from 表名 where age>25 and age<31;
查询某个字段在指定集合中的数据:select* from表名where classin(1833,1834);
查询某个字段不在指定集合中的数据:select *from 表名whereclassnotin(1833,1834);
查询某个字段为空的数据:select *from表名 whereclassisnull;
查询某个字段不为的数据: select *from表名 whereclassisnotnull;
查询某个字段模糊匹配:select *from 表名 namelike‘%ao%’;
查询限定的数量行数:select*from 表名 where 字段limit2,5; (指查看3-7行)
select*from 表名 where 字段limit4;(指查看前4行)
注意:用limit时,前面的字段一定一般接int 或者bigint类型,并且字段对应的数据有多个不同的值。
查询数据根据某个字段从小到大顺序: select*from 表名orderby字段asc;
查询数据根据某个字段从小到大顺序: select*from 表名orderby字段desc;
mysql聚合函数
统计查询数据的量:selectcount(*)from表名;
selectcount(1)from表名;
查询某个字段的求和:selectsum(字段) from表名;
查询某个字段求平均成绩:selectavg(字段) from表名;
查询某个字段求最大值: selectmax(字段)from表名;
查询某个字段求最小值: select min(字段)from 表名;
查询某个字段去重复: select distinct(字段)from表名;
查询每个班级的数学总成绩:selectsun(math)from表名group byclass;
或:selectclass,sum(math)from表名groupbyclass;
注意:涉及到求每个.......的,都需要用到group by
查询每个班级的数学总成绩总分大于100分的信息:
select class,sum(math)from表名group by classhavingsum(math)>100;
给查询的字段取别名:selectclass as'班级',sum(math)sfrom表名group byclasshavings >100;
或:selectclass as'班级',sum(math)assfrom表名group byclasshavings >100;
查询每个班级中性别为1的数学总成绩总分大于100分的信息:
selectclass,sum(math)from 表名 wheresex=1group byclass havingsum(math)>100;
单表查询总结:
1、where不能放在groupby后面
where子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组前过滤数据,条件中不能包含聚合函数,比如sum(),avg()等,使用where条件显示特定的行。
2、having是跟group by连在一起的,放在groupby后面,此时的作用相当于where
having子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚合函数,使用having条件显示特定的组,也可以使用多个分组标准进行分组。
表中插入数据:insert into 表名values(11,'xiaoliu',2021,66.66,'2021-11-21');
insert into表名(字段,字段)values(11,22);
删除表中的数据:deletefrom表名 wheresid=11;(不释放空间)
删除整张表内的数据:truncate表名;(查询表内的数据为空)
删除表格:droptable表名;(定义和内容都删除,释放空间)
更新表中的数据:update表名setmath=100 wheresid=1;
数据库、表、数据:mysql数据库里有多个数据库,可以自己创建和删除数据库,一个数据库里可以有多个表,一个表里可 以有不同的数据。
备份student表:createtablestudent1likestudent;
备份表的数据:insert into student1 select*fromstudent;
把一个表的某些字段插入新表:createtable student2likestudent;
insert into student2(id,name)select id,namefrom student;
备份数据库:(先退出mysql)
mysqldump-uroot-p123456dcs10>/dcs10.sql;
还原数据库:先进入数据库 mysql-uroot-p123456
创建一个新的数据库:create databasedcs10_back;
再退出数据库:ctal+c
再还原:mysql-uroot-p123456dcs10_back<dcs10.sql;
页:
[1]