zoukankan      html  css  js  c++  java
  • MySQL(六)

    自关联

    • 设计省信息的表结构provinces
      • id
      • ptitle
    • 设计市信息的表结构citys
      • id
      • ctitle
      • proid
    • citys表的proid表示城市所属的省,对应着provinces表的id值
    • 问题:能不能将两个表合成一张表呢?
    • 思考:观察两张表发现,citys表比provinces表多一个列proid,其它列的类型都是一样的
    • 意义:存储的都是地区信息,而且每种信息的数据量有限,没必要增加一个新表,或者将来还要存储区、乡镇信息,都增加新表的开销太大
    • 答案:定义表areas,结构如下
      • id
      • atitle
      • pid
    • 因为省没有所属的省份,所以可以填写为null
    • 城市所属的省份pid,填写省所对应的编号id
    • 这就是自关联,表中的某一列,关联了这个表中的另外一列,但是它们的业务逻辑含义是不一样的,城市信息的pid引用的是省信息的id
    • 在这个表中,结构不变,可以添加区县、乡镇街道、村社区等信息
    • 创建areas表的语句如下:
    create table areas(
    id int primary key,
    atitle varchar(20),
    pid int,
    foreign key(pid) references areas(id)
    );
    • 从sql文件中导入数据
    source areas.sql;
    • 查询一共有多少个省
    • 查询省的名称为“山西省”的所有城市
    select city.* from areas as city
    inner join areas as province on city.pid=province.id
    where province.atitle='山西省';
    • 查询市的名称为“广州市”的所有区县
      select dis.*,dis2.* from areas as dis
      inner join areas as city on city.id=dis.pid
      left join areas as dis2 on dis.id=dis2.pid
      where city.atitle='广州市';

    子查询

    • 查询支持嵌套使用
    • 查询各学生的语文、数学、英语的成绩
    select sname,
    (select sco.score from scores sco inner join subjects sub on sco.subid=sub.id where sub.stitle='语文' and stuid=stu.id) as 语文,
    (select sco.score from  scores sco inner join subjects sub on sco.subid=sub.id where sub.stitle='数学' and stuid=stu.id) as 数学,
    (select sco.score from  scores sco inner join subjects sub on sco.subid=sub.id where sub.stitle='英语' and stuid=stu.id) as 英语
    from students stu;

    字符串函数

    • 查看字符的ascii码值ascii(str),str是空串时返回0
    select ascii('a');
    
    • 查看ascii码值对应的字符char(数字)
    select char(97);
    
    • 拼接字符串concat(str1,str2...)
    select concat(12,34,'ab');
    
    • 包含字符个数length(str)
    select length('abc');
    
    • 截取字符串
      • left(str,len)返回字符串str的左端len个字符
      • right(str,len)返回字符串str的右端len个字符
      • substring(str,pos,len)返回字符串str的位置pos起len个字符
    select substring('abc123',2,3);
    
    • 去除空格
      • ltrim(str)返回删除了左空格的字符串str
      • rtrim(str)返回删除了右空格的字符串str
      • trim([方向 remstr from str)返回从某侧删除remstr后的字符串str,方向词包括both、leading、trailing,表示两侧、左、右
    select trim('  bar   ');
    select trim(leading 'x' FROM 'xxxbarxxx');
    select trim(both 'x' FROM 'xxxbarxxx');
    select trim(trailing 'x' FROM 'xxxbarxxx');
    
    • 返回由n个空格字符组成的一个字符串space(n)
    select space(10);
    
    • 替换字符串replace(str,from_str,to_str)
    select replace('abc123','123','def');
    
    • 大小写转换,函数如下
      • lower(str)
      • upper(str)
    select lower('aBcD');
    

    数学函数

    • 求绝对值abs(n)
    select abs(-32);
    
    • 求m除以n的余数mod(m,n),同运算符%
    select mod(10,3);
    select 10%3;
    
    • 地板floor(n),表示不大于n的最大整数
    select floor(2.3);
    
    • 天花板ceiling(n),表示不小于n的最大整数
    select ceiling(2.3);
    
    • 求四舍五入值round(n,d),n表示原数,d表示小数位置,默认为0
    select round(1.6);
    
    • 求x的y次幂pow(x,y)
    select pow(2,3);
    
    • 获取圆周率PI()
    select PI();
    
    • 随机数rand(),值为0-1.0的浮点数
    select rand();
    
    • 还有其它很多三角函数,使用时可以查询文档

    日期时间函数

    • 获取子值,语法如下
      • year(date)返回date的年份(范围在1000到9999)
      • month(date)返回date中的月份数值
      • day(date)返回date中的日期数值
      • hour(time)返回time的小时数(范围是0到23)
      • minute(time)返回time的分钟数(范围是0到59)
      • second(time)返回time的秒数(范围是0到59)
    select year('2016-12-21');
    
    • 日期计算,使用+-运算符,数字后面的关键字为year、month、day、hour、minute、second
    select '2016-12-21'+interval 1 day;
    
    • 日期格式化date_format(date,format),format参数可用的值如下

      • 获取年%Y,返回4位的整数

        * 获取年%y,返回2位的整数

        * 获取月%m,值为1-12的整数

      • 获取日%d,返回整数

        * 获取时%H,值为0-23的整数

        * 获取时%h,值为1-12的整数

        * 获取分%i,值为0-59的整数

        * 获取秒%s,值为0-59的整数

    select date_format('2016-12-21','%Y %m %d');
    
    • 当前日期current_date()
    select current_date();
    
    • 当前时间current_time()
    select current_time();
    
    • 当前日期时间now()
    select now();
  • 相关阅读:
    Educational Codeforces Round 69 D E
    洛谷P2325 [SCOI2005]王室联邦
    洛谷P1494 [国家集训队]小Z的袜子
    洛谷P4514 上帝造题的七分钟
    POJ3321Apple Tree
    满血复活前的记录(持续更新ing)
    各位2019见
    LuoguP3338 [ZJOI2014]力
    Codeforces Round #478 Div2 975A 975B 975C 975D
    Codeforces Round #479 (Div. 3) 题解 977A 977B 977C 977D 977E 977F
  • 原文地址:https://www.cnblogs.com/leecoffee/p/9038354.html
Copyright © 2011-2022 走看看