zoukankan      html  css  js  c++  java
  • 学习Microsoft SQL Server 2008技术内幕:T-SQL语法基础--第4章

    第4章 子查询

    4.2.1 Exist 谓语:

    use TSQLFundamentals2008
    select * 
    from Sales.Customers as C
    where c.country=N'Spain'
    
    select * 
    from Sales.Customers as C
    where c.country=N'Spain' and exists(select * from Sales.Orders as O where o.custid=C.custid)
    
    select * 
    from Sales.Customers as C
    where c.country=N'Spain' and not exists(select * from Sales.Orders as O where o.custid=C.custid)
    View Code

     

     4.3.2 连续聚和

    select OBJECT_ID('Sales.OrderTotalsByYear')
    if OBJECT_ID('Sales.OrderTotalsByYear') is not null  drop view Sales.OrderTotalsByYear
    
    go
    create view Sales.OrderTotalsByYear with schemabinding 
    as 
      select 
        YEAR(o.orderdate) as orderyear,
        SUM(od.qty) as qty  
      from 
      Sales.Orders as o
        join Sales.OrderDetails as od on o.orderid=od.orderid
      group by YEAR(o.orderdate)
    go
    View Code

     查询:

    select O1.orderyear, o1.qty,
        (select  SUM(o2.qty) from 
          Sales.OrderTotalsByYear as o2
          where o2.orderyear <= o1.orderyear
        ) as runqty
    from  Sales.OrderTotalsByYear as O1
    order by o1.orderyear
    View Code

    4.3.3 行为不当的子查询

    SELECT custid, companyname
    FROM Sales.Customers AS C
    WHERE custid NOT IN(SELECT O.custid
                        FROM Sales.Orders AS O);
    View Code

    返回:

    custid   companyname
    22        Customer DTDMN
    57        Customer WVAXS

    插入一条custid=NULL的记录:

    INSERT INTO Sales.Orders
      (custid, empid, orderdate, requireddate, shippeddate, shipperid,
       freight, shipname, shipaddress, shipcity, shipregion,
       shippostalcode, shipcountry)
      VALUES(NULL, 1, '20090212', '20090212',
             '20090212', 1, 123.00, N'abc', N'abc', N'abc',
             N'abc', N'abc', N'abc');
    View Code
    SELECT custid, companyname
    FROM Sales.Customers AS C
    WHERE custid NOT IN(SELECT O.custid
                        FROM Sales.Orders AS O);
    View Code

    返回0行

    原因:

          NOT(22=1 Or 22=2 Or 22=NULL)

          NOT(False or False Or UnKnown)

          NOT UnKnow

          Unknow

          查询条件过滤UnKnown

    而:

    SELECT custid, companyname
    FROM Sales.Customers AS C
    WHERE  not exists (SELECT *
                        FROM Sales.Orders AS O where C.custid=O.custid);
    View Code

    SELECT custid, companyname
    FROM Sales.Customers AS C
    WHERE custid NOT IN(SELECT O.custid
                        FROM Sales.Orders AS O where O.custid is NOT null);
    View Code

    返回:

    custid   companyname
    22        Customer DTDMN
    57        Customer WVAXS

  • 相关阅读:
    购买绝版书的好地方——淘宝
    ASP.NET MVC轻教程 Step By Step 1 ——入门
    ASP.NET MVC轻教程 Step By Step 2 ——View初探
    快速启动WebDev.WebServer的方法
    Surface RT使用手记
    ASP.NET MVC轻教程 Step By Step 3 ——使用ViewBag
    Asp.net MVC分页实例
    图示近四年来国外主流编程语言发展趋势
    ASP.NET MVC轻教程 Step By Step 4——Model、View和Controller
    Asp.net MVC使用KindEditor4
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/5555644.html
Copyright © 2011-2022 走看看