第七章 数据查询

第七章  数据查询
1.掌握select 查询语句的基本格式
    select 字段 from 表名  where 条件
    group by 分组
    having 条件
    order by 排序dec | desc
    limit 限制查询行数
    into outfile 保存查询结果;

2.掌握基本的无条件查询
    select * from 表名;

3.掌握条件查询的使用
    select * from xsjbxxb where xb='女';
    别名设置:select 字段1 as 别名 from xsjbxxb;
        eg:select  bjbh as 班级编号 from xsjbxxb;

    带in关键字查询:select xh,kcdm from xsxkb where in('00202117','00202118');
    带between and 关键字查询:select xh,kcdm,cj from xsxkb where cj between 80 and 90;
    带like 的模糊查询:select xh,xm from xsjbxxb where xm like '张%';
    带is null 的空查询:select * from xsxkb where cj is null;
    带and | or的多条件查询:select xh,xm,xb,csrq from xsjbxxb where xb='男' and csrq>='2000-1-1';

4.掌握使用聚合函数实现数据的统计
    常用聚合函数:count,sum,avg,max,min
    count: select count(*) as 学生人数 from xsjbxxb;
    sum: select sum(cj) as 总成绩 from xsxkb where xh='201820107101';
    avg: select avg(cj) as 平均成绩 from xsxkb where xh='201820107101';
    
5.掌握分组和排序查询的使用
    分组查询(和聚合函数配合使用):注意select 查询的数据只能是group by分组的字段,或使用了聚合函数的非分组字段。
             group by <组合表达式> [with rollup] [having where_condition];    提示:with rollup 表示对分组的数据进行分类汇总。

        select xb as 性别,count(*) as 人数
        from xsjbxxb
        group by 1;
    
        select xsxkb.xh as 学号,count(*) as 选课门数
        from xsxkb,xsjbxxb
        where xsxkb.xh=xsjbxxb.xh
        and xb='男'
        group by xsxkb.xh
        having count(*)>=3;

    order by 排序查询,limit限制查询行数
           order by <组合表达式> [asc | desc];

        select xh,xm,xb,csrq
        from xsjbxxb
        where xb='女'
        order by csrq desc
        limit 3;
    
6.掌握多表查询的使用
         内连接:只返回满足连接条件的数据行(join ... on ...)
    select xs.xh,xm,kcmc,cj
    from xsjbxxb as xs join xsxkb
    on xs.xh=xsxkb.xh
    join kcdmb on xsxkb.kcdm=kcdmb.kcdm
    where xs.xh='201820107101';
        外连接:
               左外连接:结果集包括满足条件的行外,还包括左表中不满足条件的记录。
                               select xs.xh,xm,kcdm,cj
                               from xsjbxxb as xs left join xsxkb
                               on xs.xh=xsxkb.xh
                               where xs.xh='201820901102';

               右外连接:结果集包括满足条件的行外,还包括右表中不满足条件的记录。(right join ......on......)
                               select xs.xh,kcdm,cj
                               from xsxkb right join xsjbxxb as xs
                               on xsxkb.xh=xs.xh
                               where xb='女';
    交叉连接:产生笛卡尔积。(cross ..... join.....)
         select *
         from bmdmb cross join jsjbxxb;
    自然连接:就是一个表的两个副本之间的连接(同一个表在from子句中出现两次,为了区分必须对表指定不同的别名)
            查询和魏志强同专业的学生。
                     select xs1.xh,xs1.xm 
                     from xsjbxxb as xs1,xsjbxxb as xs2
                     where xs1.zymc=xs2.zymc and xs2.xm='魏志强'
                     and xs1.xm!='魏志强';

7.掌握子查询的使用
答:子查询就是在一个查询语句中,还包括另一个查询语句。在外一层的查询中使用里面一层查询产生的结果集。它可以嵌套在select,insert,update,delete等语句或
其他子查询中。
    比较子查询:select xh,cj
            from xsxkb
           where kcdm=(select kcdm from kcdmb where kcmc='微机原理与应用');
    带in的关键子查询:select xh,xm
                     from xsjbxxb
                     where xh in(select xh from xsxkb where cj>90);
    批量比较子查询:all:指定表达式要与子查询结果集中的每个值进行比较,只有当表达式的值都满足比较关系时,才能返回true。
                  some ,any:只要与子查询结果集中的某个值满足比较关系时,就会返回true。
               
                select xm from xsjbxxb
                where xh=some(select xh from xsxkb where cj <60);
    exists子查询:该子查询不需要返回任何数据,只是返回一个逻辑值。当内层语句查询到满足条件的记录时,就返回一个真值,否则返回一个假值。
        select xm
        from xsjbxxb 
        where exists(select * from xsxkb where xsjbxxb.xh=xsxkb.xh);

    利用子查询插入,更新,删除。
    插入:    create table xs like xsjbxxb;创建结构与xsjbxxb一致的表。
        insert into xs
        select * from xsjbxxb 
        where year(csrq)=2000;
    更新:    update xs 
        set jl='优秀'
        where xh in(select xh from xsxkb where cj>90);
    删除:    delete from xs
        where xh in(select xh from xsxkb where cj<60);
    

8.掌握正则表达式的模糊查询
答:常用字符:^,$,.,*,+,<字符串>,[字符集合],[a-z],[^],字符串{n},字符串{n,}字符串{m,n}.
    语法:where 字段名 [not] [regexp | rlike] 表达式;
    eg:select xh,xm,xb
         from xsjbxxb
         where xm regexp'^张%';

补充:合并结果集(union)
    select bjbh,bmh,bjzwmc
    from bjdmb
    where bmh='01'
    union
    select bjbh,bmh,bjzwmc
    from bjdmb
    where bmh='03';
    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ee .

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值