Skr-Eric的Mysql课堂(四)——Mysql的SQL高级查询

SQL高级查询


  1、总结
    3、select ...聚合函数 from 表名
    1、where ...
    2、group by ...
    4、having ...
    5、order by ...
    6、limit ...;


  2、order by :给查询结果排序
    1、order by 字段名 ASC/DESC
    2、ASC(默认) :升序
       DESC :降序


    练习
      1、将所有英雄按防御值从高到低排序
        select * from sanguo order by fangyu DESC;
      2、将蜀国英雄按攻击值从高到低排序
        select * from sanguo 
    where country="蜀国" order by gongji DESC;
      3、将魏蜀两国英雄中名字为3个字的,按防御值升序排序
        select * from sanguo where
    country in("魏国","蜀国") and name like "___"
    order by fangyu;

    select * from sanguo where
    (country="蜀国" or country="魏国") and name like "___" order by fangyu;

    select * from sanguo where
    (country="蜀国" and name like "___") or 
    (country="魏国" and name like "___")
    order by fangyu;


  3、limit (永远放在SQL命令的最后写)
    1、显示查询记录的条数
    2、用法
      limit n;  --> 显示 n 条记录
      limit m,n; --> 从第 m+1 条记录开始,显示 n 条
      limit 2,3 : 显示第3、4、5三条记录
    练习
      1、在蜀国英雄中,查找防御值倒数第2名到倒数第4名的英雄的姓名、防御值和国家
        select name,fangyu,country from sanguo
    where country="蜀国"
    order by fangyu ASC 
    limit 1,3;
      2、在所有蜀国名字不为NULL的英雄中,查找攻击值前3名的英雄的姓名、攻击值和国家
        select name,gongji,country from sanguo
        where
    country="蜀国" and name is not NULL
    order by gongji DESC
    limit 3;
    3、分页
      每页显示5条记录,显示第4页的内容
      每页显示n条记录,显示第m页的内容

      第1页 :limit (1-1)*5,5  # 1 2 3 4 5
      第2页 :limit (2-1)*5,5  # 6 7 8 9 10
      第3页 :limit (3-1)*5,5 # 11 12 13 14 15
      ...
      第m页 :limit (m-1)*n,n


  4、聚合函数
    1、分类
      avg(字段名) : 求该字段的平均值
      sum(字段名) : 求和
      max(字段名) : 最大值
      min(字段名) : 最小值
      count(字段名) : 统计该字段记录的个数
    2、练习
      1、所有英雄中攻击力最大值
        select max(gongji) from sanguo;
      2、统计id、name两个字段分别有多少条记录
        select count(id),count(name) from sanguo;
  5、group by : 给查询的结果进行分组
    1、查询表中都有哪些国家
      select country from sanguo group by country;
    2、计算每个国家的平均攻击力
      select country,avg(gongji) from sanguo
      group by country;
      先分组     再聚合    再去重
       蜀国       
       蜀国
       蜀国       200       蜀国
       魏国
       魏国       200       魏国
       吴国       100       吴国
    3、注意
      1、select之后的字段名如果没有在group by之后出现,则必须要对该字段进行聚合处理(聚合函数)
    4、练习
      1、查找所有国家中英雄数量最多的前2名的国家名称和英雄数量(count())
        select country,count(id) as number from sanguo
    group by country
    order by number DESC
    limit 2;


  6、having语句
    1、作用 :对查询结果进行进一步的筛选
    2、练习
      1、找出平均攻击力大于105的国家的前2名,显示国家名和平均攻击力
        1、计算每个国家的平均攻击力
    2、找到平均攻击力 > 105 的
    select country,avg(gongji) as average from sanguo
    group by country
    having average>105
    order by average DESC
    limit 2;
    3、注意
      1、having语句通常和group by语句联合使用,过滤由group by语句返回的记录集
      2、where只能操作表中实际存在字段,having语句可操作由聚合函数生成的显示列

 

7、distinct : 不显示字段的重复值
    1、语法 :select distinct 字段1,字段2 from 表名;
    2、示例
      1、表中都有哪些国家
        select distinct country from sanguo;
      2、表中一共有几个国家
        select count(distinct country) as n from sanguo;
    3、注意
      1、distinct和from之间的所有字段值都相同才会去重
  2、查询表记录时可以做数学运算
    1、运算符 :+ - * / % 
    2、示例
      1、查询时显示所有英雄攻击力翻倍
        select id,name,gongji*2 as new from sanguo;


8、约束
  1、作用 :保证数据的一致性、有效性
  2、约束分类
    1、默认约束(default)
      插入记录时,不给该字段赋值,则使用默认值
      sex enum("M","F","S") default "S",
    2、非空约束(not null)
      不允许该字段的值为 NULL
      id int not null,
      id int not null default 0,


9、索引
  1、定义
    对数据库中表的一列或多列的值进行排序的一种结构(BTree)
  2、优点
    加快数据的检索速度
  3、缺点
    1、当对表中数据更新时,索引需要动态维护,降低数据的维护速度
    2、索引需要占用物理存储空间
4、索引示例
  1、开启运行时间检测 :mysql> set profiling=1;
  2、执行查询语句
    select name from t1 where name="lucy99999"; 
  3、查看执行时间 
    show profiles;
  4、在name字段创建索引
    create index name on t1(name);
  5、再次执行查询语句
    select name from t1 where name="lucy100000";
  6、查看执行时间
    show profiles;
5、索引
  1、普通索引(index)
    1、使用规则
      1、可设置多个字段,字段值无约束
      2、把经常用来查询的字段设置为索引字段
      3、KEY标志 :MUL
    2、创建
      1、创建表时
        create table t1(
    ...,
    ...,
    index(name),
    index(id));
      2、已有表中
        create index 索引名 on 表名(字段名);
    3、查看索引
      1、desc 表名; -->KEY标志为 MUL
      2、show index from 表名\G;
    4、删除index
      drop index 索引名 on 表名;
  2、唯一索引(unique)
    1、使用规则
      1、可设置多个字段
      2、约束 :字段值不允许重复,但可以为 NULL
      3、KEY标志 :UNI
    2、创建
      1、创建表时
        unique(phnumber),
    unique(cardnumber)
      2、已有表
        create unique index 索引名 on 表名;
    3、查看、删除同普通索引
      删除 :drop index 索引名 on 表名;
  3、主键索引(primary key)&&自增长(auto_increment)
    1、使用规则
      1、只能有1个字段为主键字段
      2、约束 :字段值不允许重复,也不能为 NULL
      3、KEY标志 :PRI
      4、通常设置记录编号字段 id,能够唯一锁定一条记录
    2、创建
      1、创建表时
        1、id int primary key auto_increment,
       name varchar(20) not null
       )auto_increment=10000,charset=utf8,engine=InnoDB;
       alter table 表名 auto_increment=10000;
    2、
      id int auto_increment,
      name varchar(20),
      primary key(id)
      2、已有表
        alter table 表名 add primary key(id);
    alter table 表名 modify id int auto_increment;
    3、删除主键
      1、先删除自增长属性(modify)
        alter table 表名 modify id int;
      2、删除主键
        alter table 表名 drop primary key;

 

 

想要看更多的课程请微信关注SkrEric的编程课堂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值