• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
成为自己最想成为的那种人
博客园    首页    新随笔    联系   管理    订阅  订阅
数据查询

①正常: 

    select Sname, 'Year of Birth:', 2004-Sage, ISLOWER(Sdept)

    from Student;

②自定义表头:

    select Sname 姓名, 'Year of Birth:' 出生年份, ……

③消除重复的元组:

    select distinct Sno    //distinct作用范围是所有列

    from SC;

1> where子句常用的查询条件

    比较:    >  <  =  (特殊:not + …)

    确定范围:  between and  ( not between and )

    确定集合:  in  ( not in )

    字符匹配:  like  ( not like )

    空值:    is NULL  ( is nut null )

    多重条件:  and, or

    

        where Sdept IN ('CS', 'MA', 'IS');

        where Sname Like '刘%';  ( like '欧阳_')  ( like '_阳%')

              where Cname like 'DB\ _% i _ _' escape '\';

      order by ASC (升序) / DESC (降序) 

      order by Sdept,Sage DESC;  (先按系号升序,再按年龄降序)

2> 聚集函数

    聚集函数只能用于select子句和“group by 中的 having 子句”,不能用在 where 中

      count ( * )

      count / sum / avg / max / min (列名)

    select count (*) /   select count (distinct Sno)

3> group by 子句

    对查询结果分组的目的是为了细化聚集函数的作用对象。

    分组后聚集函数将作用于每一个组。

    如果分组后还要求按一定的条件对这些组进行筛选,最终只输出满足指定条件的组。

    having 短语作用于组。

  查询选修了3门以上课程的学号:

      select Sno

      from SC

      group by Sno

        having count (*) > 3;

4> 自身连接、外连接、复合条件连接

       “连接”的关键点在于 where 子句,同时, from 一般也包含多个表

  自身连接:

        select first.Cno, second.Cpno

        from Course first, Course second

        where first.Cpno = second.Cno

  外连接:

      外连接以指定表为连接主体,将主体表中(不)满足连接条件的元组一并输出

        where Student.Sno = SC.Sno(*);

  复合条件连接:

        where Student.Sno = SC.Sno AND

            SC.Cno='2'  AND

            SC.Grade > 90;

5> 嵌套查询

    将一个 select - from - where 查询块嵌套在另一个查询块的 where 子句或 having 短语的条件中

        select Sname    //父查询

        from Student

        where Sno IN

          ( select Sno    //子查询  (子查询不能使用 order by 子句)

          from SC

          where Cno = '2' );

    不相关子查询:子查询的查询条件不依赖于父查询

    相关子查询:         ……依赖于……

  不相关子查询求解方法:由里向外逐层外理。

    

posted on 2017-06-04 13:38  遇见未来  阅读(318)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3