zoukankan      html  css  js  c++  java
  • SQL Server 基础 05 多链表查询和子查询

                                  

                                                                     连接查询

    值得注意的是:字段前必须加表名,以便混淆

    1 -- 多表连接查询和子查询
    2 select * from dbo.stu_info ,dbo.sname2
    3 -- 加连接规则的查询 where
    4 select * from dbo.stu_info ,dbo.sname2 where dbo.stu_info.sname= dbo.sname2.姓名

    使用where前  

    使用where后

    使用逻辑运算符 and

    1 select dbo.stu_info.sno,dbo.stu_info.sname,dbo.stu_info.sex,dbo.stu_info.depart
    2 ,dbo.sname2.score,dbo.sname2.姓名,dbo.sname2.sno
    3 from dbo.stu_info,dbo.sname2
    4 where dbo.stu_info.sname='张三'
    5 and dbo.stu_info.sno=dbo.sname2.sno
    6 order by dbo.sname2.score desc

     表别名简化语句 

    1 select A.sno,A.sname,A.sex,A.depart
    2 ,B.score,B.姓名,B.sno
    3 from dbo.stu_info as A,dbo.sname2 as B
    4 where A.sname='张三'
    5 and A.sno=B.sno
    6 order by B.score desc

    多表连接查询

     跟上面的相似,只是 from 后面多了n个表名 同时多了n-1 个  and  。。。 

    使用  inner join 连接查询

          在where子句中表达式变的臃肿,为了变得清晰,让人理解,所以 ansi SQL 建议使用 inner join 进行多表连接

          语法格式 :

                       select * from 表名1 

                       inner join 表名2

                               on  连接规则1

                       inner join 表名3

                               on  连接规则2

                       . . .   . . .

                       inner join 表名n

                               on  连接规则n-1

           举例 :

           

    1 -- 使用 inner join 
    2 select * from dbo.stu_info as A    
    3          inner join dbo.sname2 as B
    4          on A.sno=B.sno
    5          where A.depart='心理学'
    6          order by B.score desc 

    /**/  相比上面的 where + and 更简单

                                                        高级连接查询

           // 这里只是粗略的概括下,来自 书本:《21 天学通SQL Server》  第11章 P226 

    1、自动连接查询

                  /**/ 

                   select 表名1.* from 表名1,表名2 where 表名1.字段=表名2.字段 and 表名2.字段=‘字段值’

    2、内连接查询

                   等值连接    : 表名1.*=表名2.*

                   自然连接    : select 后面不使用* ,而是字段名

                   不等值连接 : 由 >、>=、< 、<= 、<>  、between . . . 查询某些范围内的值

    3、左外连接查询

                   语法         :select * from 表名1 left outer join 表名2 on 表名1.字段=表名2.字段

    4、右外连接查询

                   语法         :select * from 表名1 right outer join 表名2 on 表名1.字段=表名2.字段

    5、全外连接查询

                   语法         :select * from 表名1 full outer join 表名2 on 表名1.字段=表名2.字段

    6、交叉连接查询

                   语法         :select * from 表名1 cross join 表名2

                                                       组合查询

     1 -- 使用组合查询union
     2          
     3 select * from dbo.stu_info where depart='心理学'
     4 union
     5 select * from dbo.stu_info where datediff(year,date,getdate())>30
     6 
     7 -- union 、group by 、聚合函数 三者共同使用
     8 select sno,姓名,score from dbo.sname2
     9 union
    10 select '总分:',姓名,sum(score) from dbo.sname2 group by 姓名
    11 union
    12 select '平均分:',姓名,avg(score) from dbo.sname2 group by 姓名

                                                      子查询

    select语句中中镶陶一个select语句

    语法 : 

                 select  字段1 ,字段2 from 表

                 where 字段2=(select 字段2 from 表2 where 字段3=‘字段值’)

                 order by 字段4 desc

  • 相关阅读:
    【JavaScript框架封装】数据类型检测模块功能封装
    【JavaScript框架封装】数据类型检测模块功能封装
    JavaScript进阶【五】利用JavaScript实现动画的基本思路
    JavaScript进阶【四】JavaScript中的this,apply,call的深入剖析
    JavaScript进阶【三】JavaScript面向对象的基础知识复习
    JavaScript进阶【二】JavaScript 严格模式(use strict)的使用
    JavaScript进阶【一】JavaScript模块化开发的基础知识
    OPENGL学习【一】VS2008开发OPENGL程序开发环境搭建
    WEBGL学习【十五】利用WEBGL实现三维场景的一般思路总结
    Blender软件导出的obj数据格式文件内容解读
  • 原文地址:https://www.cnblogs.com/izhiniao/p/3715232.html
Copyright © 2011-2022 走看看