zoukankan      html  css  js  c++  java
  • 查询

    Select基本查询

      select  列名  from 表名

      当列名用 * 代替时,则表示选择表的所有列

    条件查询

      select * from XS where 姓名=‘张三’

     select  姓名,出生日期  from  xs  where 出生日期>‘1992-1-3’

    注:字符型或日期型的数据要用单引号引起来

    and,or    and 同时满足两个  or 满足一个即可

      select * from xs  where  姓名=‘张三’  and  学号=‘11’

       select * from xs  where  姓名=‘张三’  or学号=‘11’

    in=or (A,B)  只表示两个值  A或B   not in 

    in用于查找属性值属于指定集合的记录,与in相对的not in

    例如:查找分数为70和80的所有学生信息

     select  *  from xs  where 总学分  in (70,80)  ==总学分=70 or 总学分=80

    between...and  在...范围之类     not betwwen....and

    例:查询分数在60到70之间

    select  *  from xs where 总学分  between  60  and  70 

    is  关键字—————专门针对Null     is not null

    例:查询邮箱为空的学生记录

      select * from student  where Email is null

    distinct   关键字

      从返回的数据集中删除重复的列

      select distinct 总学分 from xs order by  总学分

      --查询表xs中总学分,但是不能有重复的值

    使用like 字句进行模糊查询

       like 字句与通配符配合使用

    1, %:表示任意字符

    2,_:表示单个任意字符

    3,[]:表示方括号里列出的任意一个字符

    4,[^]:表示任意一个没有在方括号里列出的字符

    基本语法

      select 字段名 from 表名 where   字段名 like 条件

    order by 排序  asc 升  desc 降

       select * from xs order by 总学分,出身日期 desc 

    :以第一列为主序,再在第一列形同的基础上在对第二列排序,asc默认,可省略

    Top 关键字

      top:关键字用于指定只返回前面一定数量的数据  

     top n:表示返回最前面的N行

      例:

         select top 5*  from xs order by 总学分

           top....with ties  指定返回并列前N条记录,与order by  连用 

    聚合函数

      Count(*)     统计记录的条数

      Min(列名)   计算某一列的最小值

     Max(列名)   最大值

     SUM(列名) 某一列的总和

     AVG(列名)  某一列的平均数

    例:查询学生的个数

       select  Count(*)  from  student

       select   Count(Dept)  from student

      查询学生的总成绩和平均成绩

               select  sum(Grade)  as 总成绩,AVG(Grade)  as  平均成绩  from student  

      查询计算机系中年龄最大和最小的学生

                 select max(Age),min(Age)  from  student  where  Dept='计算机应用'

    聚合函数与分组查询 

         可使用group by 字句对查询的结果集进行分组,将查询结果表的各行按一列或多列取值相等的原则进行分组

    使用group by时,必须注意

        select 字句的字段列表中,除了聚合函数以外,其他所出现的一定要在group by字句中有定义才行

        group by 字句中不能使用字段别名

        text,ntext,image类型不能作group by 字句的分组依据

       例如:

       查询每个系有多少个学生

        select Dept,Count(SID) from student  group by Dept

      查询每个学生选课成绩的总分数和平均分

       select SID,SUM(Grade) as 总成绩,AVG(Grade) as 平均分 from student group by SID

    根据条件分局查询

        如果分组后还有按一定的条件对这些组进行筛选,最终只输入满足条件的组,则可以使用Having 短语指定筛选条件

    例如:

      查询学生超过3个的系

      select dept,Count(SID)  from  student  group by dept having Count(SID)>3

    注:

      where字句与Having短语的根本区别在于作用对象不同

      where字句作用于基本表或视图,在分组之前

     haing短语作用于组,在分组之后

      聚合函数只能在having中,不能再where中

    select 语句标准语法小结

       select <查询目标列组> from  <参与查询的表>  

       [where  <查询条件> ]

       [group by <分组表达式>]   [Having  <分组查询条件>]

       [order by  [排序表达式]  [Asc|Desc]  ] 

    例如:

       select SID as 学号,Count(CID) as 选课门数,Sum(Grade) as 总成绩,AVG(Grade) as 平均成绩  from  student 

      where  SID<=11  group by SID  having  Count(CID)>=2 and avg(Grade)>60  order  by  avg(Grade)  desc

    嵌套查询

      保存查询结果    

        select 字段列表  into   新表名   from 原表名   where  查询条件

      比较符

        =,!=(<>),>,>=,<,<=

        一个select....from ....where  语句称为一个查询块

           将一个查询块嵌套在另一个查询块的where字句或having短语的条件中的查询称为嵌套查询

    例如:

       select  <目标列> from 表 where  表达式(列名)   比较符  (select  列 from 表 [where .....] )

      some和any:只要与子查询结果集中的某个值满足比较的关系时,就返回true,否则返回false

     All:要与子查询结果每个值进行比较,当表达式与每个值都满足比较的关系时,才返回true,否则返回false

      例如:

      查找比所有计算机系的学生年龄都大的学生

       select * from xs where 出生日期>All (select 出生日期  from xs where 专业=‘计算机‘)

     in:  单查询的结果在子查询结果集中国则为True

        select  <目标列>  from 表  where  列名 in  (select  列  from  表 [where.....] )

    Exists:如果子查询返回行则真,否则假

      select <目标列>  from  表  where  exists  (select  列  from  表  [where....])

    连接查询

       根据各个数据表之间的逻辑关系从二个或多个数据表中检索数据

    注: 1,连接的两个字段必须数据类型和值域相同

          2,尽量在表的主键上的基础上,指定连接条件

       连接查询分类:

        内连接:基本内连接,多表连接,自连接

        外连接:左外连接右外连接,全外连接,交叉连接

    内连接方式:

      select 字段1.字段2,。。。。 from 表1  join 表2  on 连接条件

    例如:

      select * from xs  join  xk  on  xs.学号=xk.学号

    多表连接:

       select ..... from 表1 join 表2 on  条件1

                                  join 表3  on  条件2

                                  join 表4  on   条件3

                                    .。。。。。。。。。。

      或者   select .... from 表1,表2  where 条件

    例如:

       select  姓名,课程名,成绩  from xs  join  xk  on  xs.学号=xk.学号   join kc  on kc.课程号=xk.课程号

    自连接:比较一个表中各个记录之间的值

       select  a.学号,a.课程号,a.成绩  from  xk  a join  xk b  on a.成绩=b.成绩  and  a.课程号<>b.课程号

    左外连接:left outer  join      左外连接结构集中除了包括满足条件的行外,海包括左表所有的行

       select  xs.*,课程号  from  xs left  outer  join  xk  on xs.学号=xk.学号

    右外连接:right outer join 结构集中除了包括满足的条件的行外,还包括右表的所有行 

         select  xs.*,课程号  from  xs right outer  join  xk  on xs.学号=xk.学号

    全外连接:full outer  join  结构集中除了包括满足的条件的行外,还包括两个表的所有行 

          select  xs.*,课程号  from  xs full outer  join  xk  on xs.学号=xk.学号

    交叉连接:cross  join  (乘集)  不能带where 语句,可以多张表

        从被连接的表中返回所有可能的记录组合(如:一张表8条记录,另一张9条记录,结果返回72条记录)

      select 字段1,字段2,.....

       from 表1 cross  join  表2

  • 相关阅读:
    .NET 6.0 —— 网络监视器 (TODO)
    Google adwords api —— report & AWQL
    Linux 镜像更新 为国内镜像源 for debian
    优化代码 —— 二八法则 & 编完代码,再优化
    鳥哥的 Linux 私房菜 ——— 第十八章、 服务的防火墙管理 xinetd, TCP Wrappers(3)
    端口号port 是什么
    aptget的install、update、upgrade的区别(转发)
    Google ads api —— github
    .net 6.00 —— record 类型 (TODO)
    Compiled models —— .NET Core 6.0
  • 原文地址:https://www.cnblogs.com/Sea1ee/p/6359269.html
Copyright © 2011-2022 走看看