找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

like limit 排序 分组 having子句 备份数据库和表

[复制链接]
(5)查询某个字段为空,用where is null
       select name from student where age is null;从表中查询年龄为空的学生的名字;
      查询为空时,where语句里用is null,不能用age≠null
15.like子句:模糊匹配,主要做一些简单的查询或者筛选,意思“像”,表达式主要是负责2个符号;
    (1)百分号:%,用百分号可以匹配多个任意字符,可以表示一个字符,2个字符,3个字符,n个字符等,一般大多数用的是*号
  比如:select name from student where name like "张%";查询表中名字以张开头的名字(查询表中以张开头的名字)
          select name from student where name like "%张";查询表中名字以张结尾的人的名字
      select name from student where name like "%张%";查询表中名字含有张字的人的名字
       select 字段 from 表名 where 字段 like “张%”;查询表中字段以张开头的内容
(2)_:“_”,下划线,表示匹配固定一个字符,一个下划线“_”表示一个字符,2个下划线“__”表示2个字符,3个下划线"___"表示3个字符
      select name from student where name like "程__";查询表中名字中为程某某的人的名字(用了2个下划线)
      select name from student where name like "程_";查询表中名字为程某格式的人的名字(用了1个下划线)
         select name from student where name like "_程_";查询表中名字为某程某格式的人的名字(用了2个下划线)
        select name from student where name like "__程_";查询表中名字为某某程某格式的人的名字(用了3个下划线)
      select 字段 from 表名 where 字段 link "___";(用了3个下划线)查询表中名字为某某某格式的人的名字
16.limit m,n   :查询表中从m+1行开始,往下数n行的内容,包含m+1行(注意:m表示的是国外的记行方式,国外第一行数据是从0行开始数的),中国说法也就是查看第m+1行到第m+n行的内容
     查询表中第2行到第6行的数据    select * from student limit 1,5;
     查询表中第4行到第7行的数据     select * from student limit 3,4;
      
17.排序和分组
   (1)排序:order by命令,用order by +字段名+升降序;表示表中数据以某字段从大到小排列(或者表中的数据以从小到大排列)降序:desc ,升序:asc,不过一般系统默认排序为升序,所以升序时,asc可加可不加
       单字段排序:select * from student order by age desc;将表中的数据按照年龄从大到小进行排列
         select * from student order by age asc;将表中的数据按照年龄从小到大进行排列
         select * from student order by age;将表中的数据按照年龄从小到大进行排列(系统一般默认为升序)
       多字段排序:select * from student order by class asc,age desc;将表中的数据按照班级从小到大排列,每个班按照年龄从大到小排列
       select * from student order by class,grade desc;将表中数据按照班级从小到大排列,每个班按照成绩从高到底排列;
  (2)分组:group by命令,指的是针对返回数据进行分组,并只展示每个分组的第一条数据记录,其他数据不显示,隐藏,一般分组与聚合函数多在一起使用,分组的意义是为了统计和估量每个分组下的数据,比如分组之后的总成绩,平均成绩,统计人数等

聚合函数:又叫聚组函数,一般聚合函数后面都有括号,括号里填写参数,比如:字段等。常与分组group by 组合使用,聚合函数后面不能加其他字段,如果要加字段,也只能加group by 后面的字段
①max():求最大值,max(字段)
       select max(age) from student; 求最大年龄
       select max(age),class from student group by class;将表中数据进行分班,并求出每个班级中的最大年龄(求每个班的最大年龄)
②min():求最小值,min(字段)  
      select min(age),class from student group by class;求每个班级的最小年龄
③avg():求平均值,avg(字段)
       select avg(age),class from student group by class;求每个班的年龄平均值
④sum():求和,sum(字段)
     select sum(age),class from student group by class;求每班的年龄之和
⑤count():统计,count(*)一般count后面括号里的参数为*号,表示统计所有的人数,若是括号里加的是字段,比如count(name)的话,在统计的时候,如果有姓名为空的字段,则不会统计在内,加字段会漏测
          select count(*),class from student group by class; 求各班的人数
⑥distinct():去重,distinct(字段),不是聚合函数,只是一个函数,比如去掉表中班级重复的数据
           select distinct(class) from student:去掉表中班级重复的数据,也就是统计表中数据都有哪些班级
⑦floor():去浮点,也去掉小数点,不是聚合函数,只是函数,一般针对的是平均数使用
             select floor(avg(age)),class from student group by class; 求每班的平均年龄,并去除小数点
       select count(*),sex from student group by sex;统计表中男女人数

having:having句子和where相似,都是条件字句。不同之处:是一个过滤数据申明,用在group by 以及聚合函数之后,是对数据分组,聚合之后,进行数据过滤,having句子中可包含聚合函数
where:条件从句。不同之处:是一个约束申明,是发生在group by 之前,主要在在分组前对数据进行条件约束,去除不符合要求的数据,where句子中不包含聚合函数,因为where句子执行在聚合函数之前;(对聚合函数有要求的问题,一般都会有having句子)
        select sum(age),class from student group by class having sum(age)>60;求年龄之和大于60的班级
select sum(age),class from student where sex=1 group by class having sun(age)>60;求男生年龄之和大于60的班级

数据库和数据表的备份
表:
1.需要先备份创建一个新表,表结构(也就是表字段)一样,用like
      create table student1 like student;备份一个新表,就和旧表字段一样
       create table 新表名 like 旧表名;
2.然后再新表中备份数据    用insert into 新表(字段) select 字段 from 旧表(备份旧表中的数据到新表)
          insert into student1 select * from student;(把student表中的数据全部都备份到student1中)
          insert into student1(name,sex) select name,sex from student;(将旧表中的name,sex字段数据备份到新表中)
     注意:数据写入的表必须是存在的,且没有任何数据
库:无论是备份数据库还是还原数据库,都是在linux命令窗口执行
备份数据库,用mysqldump和>和.sql文件后缀;还原数据库,要用mysql和<和.sql文件后缀
备份数据库    mysqldump -uroot -p123456 class21>/home/dcs.sql(把数据库中的class21数据库备份在home目录名下的dcs中),dcs.sql为不存在的文件
       mysqldump -uroot -p密码 备份数据库名字>file1.sql
还原数据库:用mysql和<和.sql文件后缀
     1.需要在数据库中先建立一个新的数据库   
         create database duoceshi;
    2.在linux命令窗口输入还原命令
     mysql -uroot -p123456 duoceshi</home/dec.sql(把home目录名下的dcs.sql文件还原到数据库duoceshi的数据库)数据库duoceshi必为空数据库
        mysql -uroot -p密码 新建数据库名<file1.sql
分享至 : QQ空间
收藏

0 个回复

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