zoukankan      html  css  js  c++  java
  • 数据库03

    左连接

    select * from 表1 left join 表2 on 表1.列=表2.列

    • 例1:查询所有学生的成绩,包括没有成绩的学生
    select 
        * 
    from
        students stu 
    left join scores sc on stu.studentNo = sc.studentNo
    

    • 例2:查询所有学生的成绩,包括没有成绩的学生,需要显示课程名
    select  
        *
    from
        students stu 
    left join scores sc on stu.studentNo = sc.studentNo 
    left join courses cs on cs.courseNo = sc.courseNo
    

    右连接

    select * from 表1 right join 表2 on 表1.列=表2.列

    添加两门课程
    insert into courses values (0, '语文'), (0, '数学');

    • 例1:查询所有课程的成绩,包括没有成绩的课程
    select
        *
    from
        scores sc 
    right join courses cs on cs.courseNo = sc.courseNo
    
    • 例2:查询所有课程的成绩,包括没有成绩的课程,包括学生信息
    select   
        * 
    from 
        scores sc
    right join courses cs on cs.courseNo = sc.courseNo 
    left join students stu on stu.studentNo = sc.studentNo
    

    自关联

    • 设计省信息的表结构provinces
      • id
      • ptitle
    • 设计市信息的表结构citys
      • id
      • ctitle
      • proid
    • citys表的proid表示城市所属的省,对应着provinces表的id值
    • 问题:能不能将两个表合成一张表呢?
    • 思考:观察两张表发现,citys表比provinces表多一个列proid,其它列的类型都是一样的
    • 意义:存储的都是地区信息,而且每种信息的数据量有限,没必要增加一个新表,或者将来还要存储区、乡镇信息,都增加新表的开销太大
    • 答案:定义表areas,结构如下
      • id
      • atitle
      • pid
    • 因为省没有所属的省份,所以可以填写为null
    • 城市所属的省份pid,填写省所对应的编号id 这就是自关联,表中的某一列,关联了这个表中的另外一列,但是它们的业务逻辑含义是不一样的,城市信息的pid引用的是省信息的id
    • 在这个表中,结构不变,可以添加区县、乡镇街道、村社区等信息

    准备数据

    create table areas( 
    aid int primary key, 
    atitle varchar(20), 
    pid int 
    );
    
    insert into areas 
    values 
    ('130000', '河北省', NULL), 
    ('130100', '石家庄市', '130000'), 
    ('130400', '邯郸市', '130000'), 
    ('130600', '保定市', '130000'), 
    ('130700', '张家口市', '130000'), 
    ('130800', '承德市', '130000'), 
    ('410000', '河南省', NULL), 
    ('410100', '郑州市', '410000'), 
    ('410300', '洛阳市', '410000'), 
    ('410500', '安阳市', '410000'),
    ('410700', '新乡市', '410000'), 
    ('410800', '焦作市', '410000');
    

    例1:查询一共有多少个省
    select count(*) from areas where pid is null;

    例1:查询河南省的所有城市

    select
        *
    from  
        areas as p 
    inner join areas as c on c.pid=p.aid 
    where     
        p.atitle='河北省';
    

    添加区县数据

    insert into areas values 
    ('410101', '中原区', '410100'), 
    ('410102', '二七区', '410100'), 
    ('410103', '金水区', '410100');
    

    例2:查询郑州市的所有区县

    select  
        *
    from  
        areas as c 
    inner join areas as a on a.pid=c.aid
    where    
        c.atitle='郑州市';
    

    例3:查询河南省的所有区县

    select    
        * 
    from   
        areas as p 
    left join areas as c on c.pid=p.aid
    left join areas as a on a.pid=c.aid 
    where    
        p.atitle='河南省'
    

    子查询

    在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句

    主查询

    主要查询的对象,第一条 select 语句

    主查询和子查询的关系

    子查询是嵌入到主查询中 子查询是辅助主查询的,要么充当条件,要么充当数据源 子查询是可以独立存在的语句,是一条完整的 select 语句

    子查询分类

    • 标量子查询: 子查询返回的结果是一个数据(一行一列)
    • 列子查询: 返回的结果是一列(一列多行)
    • 行子查询: 返回的结果是一行(一行多列)
    • 表级子查询: 返回的结果是多行多列

    标量子查询

    • 例1:查询班级学生的平均年龄
      查询班级学生平均年龄 select avg(age) from students
      查询大于平均年龄的学生 select * from students where age > 21.4167
      select * from students where age > (select avg(age) from students);

    • 例2:查询王昭君的成绩,要求显示成绩
      学生表中查询王昭君的学号 select studentNo from students where name = '王昭君'
      成绩表中根据学号查询成绩 select * from scores where studentNo = '001'
      select * from scores where studentNo = (select studentNo from students where name = '王昭君')

    列级子查询

    • 例3:查询18岁的学生的成绩,要求显示成绩
      学生表中查询18岁的学生的学号 select studentNo from students where age=18
      成绩表中根据学号查询成绩select * from scores where studentNo in ('002','006')
      select * from scores where studentNo in (select studentNo from students where age=18)

    行级子查询

    • 例4:查询男生中年龄最大的学生信息
      select * from students where sex='男' and age=(select max(age) from students)
      select * from students where (sex,age)=('男',30)
      select * from students where (sex,age) = (select sex,age from students where sex='男' order by age desc limit 1)

    表级子查询

    • 例5:查询数据库和系统测试的课程成绩
    select  
        *
    from  
        scores s 
    inner join (select * from courses where name in ('数据库','系统测试')) c 
    on s.courseNo = c.courseNo
    

    子查询中特定关键字使用

    • in 范围

      • 格式: 主查询 where 条件 in (列子查询)
    • any | some 任意一个

      • 格式: 主查询 where 列 = any (列子查询)
      • 在条件查询的结果中匹配任意一个即可,等价于 in
    • all

      • 格式: 主查询 where 列 = all(列子查询) : 等于里面所有
      • 格式: 主查询 where 列 <>all(列子查询) : 不等一其中所有

    select * from students where age in (select age from students where age between 18 and 20)

    练习

    准备数据

    create table goods( 
    id int unsigned primary key auto_increment, 
    name varchar(150), 
    cate varchar(40),  
    brand_name varchar(40), 
    price decimal(10,3) default 0,  
    is_show bit default 1,  
    is_saleoff bit default 0 
    ); 
    
    insert into goods values(0,'r510vc 15.6英寸笔记本','笔记本','华硕','3399',default,default ); 
    insert into goods values(0,'y400n 14.0英寸笔记本电脑','笔记本','联想','4999',default,defa ult); 
    insert into goods values(0,'g150th 15.6英寸游戏本','游戏本','雷神','8499',default,default ); 
    insert into goods values(0,'x550cc 15.6英寸笔记本','笔记本','华硕','2799',default,default );
    insert into goods values(0,'x240 超极本','超级本','联想','4999',default,default); 
    insert into goods values(0,'u330p 13.3英寸超极本','超级本','联想','4299',default,default) ; 
    insert into goods values(0,'svp13226scb 触控超极本','超级本','索尼','7999',default,defaul t); 
    insert into goods values(0,'ipad mini 7.9英寸平板电脑','平板电脑','苹果','1998',default,d efault); 
    insert into goods values(0,'ipad air 9.7英寸平板电脑','平板电脑','苹果','3388',default,de fault);
    insert into goods values(0,'ipad mini 配备 retina 显示屏','平板电脑','苹果','2788',defaul t,default); 
    insert into goods values(0,'ideacentre c340 20英寸一体电脑 ','台式机','联想','3499',defau lt,default); 
    insert into goods values(0,'vostro 3800-r1206 台式电脑','台式机','戴尔','2899',default,d efault); 
    insert into goods values(0,'imac me086ch/a 21.5英寸一体电脑','台式机','苹果','9188',defau lt,default);
    insert into goods values(0,'at7-7414lp 台式电脑 linux )','台式机','宏碁','3699',default, default); 
    insert into goods values(0,'z220sff f4f06pa工作站','服务器/工作站','惠普','4288',default, default); 
    insert into goods values(0,'poweredge ii服务器','服务器/工作站','戴尔','5388',default,def ault); 
    insert into goods values(0,'mac pro专业级台式电脑','服务器/工作站','苹果','28888',default ,default);
    insert into goods values(0,'hmz-t3w 头戴显示设备','笔记本配件','索尼','6999',default,defa ult);
    insert into goods values(0,'商务双肩背包','笔记本配件','索尼','99',default,default);
    insert into goods values(0,'x3250 m4机架式服务器','服务器/工作站','ibm','6888',default,de fault); 
    insert into goods values(0,'hmz-t3w 头戴显示设备','笔记本配件','索尼','6999',default,defa ult); 
    insert into goods values(0,'商务双肩背包','笔记本配件','索尼','99',default,default);
    

    查询演练

    • 求所有电脑产品的平均价格,并且保留两位小数
      select round(avg(price),2) as avg_price from goods;

    • 查询所有价格大于平均价格的商品,并且按价格降序排序
      select id,name,price from goods where price > (select round(avg(price),2) as avg_price from goods) order by price desc;

    • 查询类型为'超极本'的商品价格
      select price from goods where cate = '超级本';

    • 查询价格大于或等于"超级本"价格的商品,并且按价格降序排列
      select id,name,price from goods where price >= any(select price from goods where cate = '超级本') order by price desc;

    数据分表

    创建“商品分类”表
    create table if not exists goods_cates( cate_id int unsigned primary key auto_increment, cate_name varchar(40) );

    查询goods表的所有记录,并且按"类别"分组
    select cate from goods group by cate;

    将分组结果写入到goods_cates数据表
    insert into goods_cates (cate_name) select cate from goods group by cate;

    通过goods_cates数据表来更新goods表
    update goods as g inner join goods_cates as c on g.cate = c.cate_name set cate = cate_id;

    通过create...select来创建数据表并且同时写入记录,一步到位
    create table goods_brands ( brand_id int unsigned primary key auto_increment, brand_name varchar(40)) select brand_name from goods group by brand_name;

    通过goods_brands数据表来更新goods数据表
    update goods as g inner join goods_brands as b on g.brand_name = b.brand_name set g.brand_name = b.brand_id;

  • 相关阅读:
    log4j日志配置
    map和java对象的转换方法
    阿里巴巴的json使用时的一些转换方法
    HttpClient发送Post和Get请求
    IT网站导航
    python学习
    git解决冲突
    协程
    Python实现协程
    异步任务神器 和定时任务Celery
  • 原文地址:https://www.cnblogs.com/albieh/p/12354582.html
Copyright © 2011-2022 走看看