zoukankan      html  css  js  c++  java
  • MySQL 聚合函数、运算符操作、约束、表的复制

    1、聚合函数

    1、分类
      avg(字段名) : 求该字段平均值
      sum(字段名) : 求和
      max(字段名) : 最大值
      min(字段名) : 最小值
      count(字段名) : 统计该字段记录的个数
    2、示例
      1、攻击力最强值是多少
        select max(gongji) from MOSHOU.sanguo;
      2、统计id 、name 两个字段分别有几条记录
        select count(id),count(name) from sanguo;
        ## 空值 NULL 不会被统计,""会被统计

      3、计算蜀国英雄的总攻击力
        select sum(gongji) from MOSHOU.sanguo
        where country="蜀国";
      4、统计蜀国英雄中攻击值大于200的英雄的数量
        select count(*) from MOSHOU.sanguo
        where gongji>200 and country="蜀国";

     

    4、运算符操作

    1、数值比较/字符比较
        1、数值比较 := != > >= < <=
        2、字符比较 := !=
        3、练习
          1、查找攻击力高于150的英雄的名字和攻击值
            select name,gongji from sanguo where gongji>150;
          2、将赵云的攻击力设置为360,防御力设置为68
            update sanguo set gongji=360,fangyu=68
            where name="赵云";

    5、查询表记录时做数学运算
      1、运算符
        + - * / % 
      2、示例
        1、查询时所有英雄攻击力翻倍
          select id,name,gongji*2 as gj from sanguo;
    2、逻辑比较
        1、and (两个或多个条件同时成立)
        2、or (任意一个条件成立即可)
        3、练习
          1、找出攻击值高于200的蜀国英雄的名字、攻击力
            select name as n,gongji as g from sanguo
            where gongji>200 and country="蜀国";
          2、将吴国英雄中攻击值为110的英雄的攻击值改为100,防御力改为60
            update sanguo set gongji=100,fangyu=60
            where country="吴国" and gongji=110;
          3、查找蜀国和魏国的英雄信息
            select * from sanguo 
            where country="蜀国" or country="魏国";
    3、范围内比较
    1、between 值1 and 值2
    2、where 字段名 in(值1,值2,...)
    3、where 字段名 not in(值1,值2,...)
    4、练习
          1、查找攻击值100-200的蜀国英雄信息
            select * from sanguo
            where gongji between 100 and 200 and
            country="蜀国";
          2、找到蜀国和吴国以外的国家的女英雄信息
            select * from sanguo
            where country not in("蜀国","吴国") 
            and sex="女";
          3、找到id为1、3或5的蜀国英雄 和 貂蝉的信息
            select * from sanguo
            where 
            (id in(1,3,5) and country="蜀国") or name="貂蝉";
          4、匹配空、非空
            1、空 :where name is null
            2、非空:where name is not null
            3、示例
              1、姓名为NULL值的蜀国女英雄信息
                select * from sanguo
                where
                name is null and country="蜀国" and sex="女";
              2、姓名为 "" 的英雄信息
                select * from sanguo where name="";
            4、注意
              1、NULL :空值,只能用 is 或者 is not 去匹配
              2、"" :空字符串,用 = 或者 != 去匹配
            5、模糊比较
              1、where 字段名 like 表达式
              2、表达式 
                1、_ : 匹配单个字符
                2、% : 匹配0到多个字符
              3、示例
              select name from sanguo where name like "_%_";
              select name from sanguo where name like "%";
              ## NULL不会被统计,只能用is、is not去匹配
              select name from sanguo where name like "___";
              select name from sanguo where name like "赵%";

    3、约束

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

    4、表的复制

    1、语法
      create table 表名 select .. from 表名 where 条件;
    2、示例
    1、复制MOSHOU.sanguo表的全部记录和字段,sanguo2
      create table sanguo2
      select * from MOSHOU.sanguo;
    2、复制MOSHOU.sanguo表的前3条记录,sanguo3
      create table sanguo3
      select * from MOSHOU.sanguo limit 3;
    3、复制MOSHOU.sanguo表的id,name,country三个字段的前5条记录,sanguo4
      create table sanguo4
      select id,name,country from MOSHOU.sanguo limit 5;
    3、复制表结构
      create table 表名 select * from 表名 where false;
    1、复制 jftab 的表结构,jftab2
      desc jftab2;
      desc jftab;
    4、注意
      复制表的时候不会把原表的 键(key) 属性复制过来

  • 相关阅读:
    程序的局部性原理2
    程序的局部性原理
    ROM
    学习Spring Security OAuth认证(一)-授权码模式
    mybatis*中DefaultVFS的logger乱码问题
    maven生命周期绑定要点
    spring security antMatchers相关内容
    JSTL
    什么是CSS hack?
    Java中获得当前静态类的类名
  • 原文地址:https://www.cnblogs.com/LXP-Never/p/9404567.html
Copyright © 2011-2022 走看看