zoukankan      html  css  js  c++  java
  • SQL Server中except和intersect用法

    except是A集合减去B集合的结果;intersect是A集合和B集合的交集;都是返回的是非重复值,很多属性都和union类似。

    还是以student为例

    select * from student;

    image

    select * into student1 from student;
    go
    insert into student1 values('aaa',20,'Js'),('bbb',30,'js'),('ccc',40,'sh');
    go
    insert into student1 values('aa',10,'JiangSu');
    go
    select * from student1 order by 1;

    image

    student1中name为aa两行是重复的;

    student1还比student多加了三行,aaa,bbb,ccc

    select * from student1
    except
    select * from student

    image

    结果只有aaa,bbb,ccc;没有aa那行

    select * from student1
    intersect
    select * from student

    image

    只有student表中的13行,aa那行没有重复

    这里只注重返回结果是没有重复行的,except和intersect 暂时没有all选项,这点和union不同。

    关于except、intersect、union的优先级验证:

    select * into student2 from student1;
    go
    delete from student2 where name='aaa';
    go
    select * from student2 order by 1;
    go

    image

    student2的表比student1表少一行aaa的记录

    select * from student2
    except
    select * from student1
    intersect
    select * from student

    image

    如果你怀疑student2记录少于student1的记录,except有问题的话,我们互换下

    select * from student1
    except
    select * from student2
    intersect
    select * from student

    image

    intersect的优先级高于except

    同样的原理测试,intersect的优先级也高于union

    except和union的级别是相同的,顺序执行的。

  • 相关阅读:
    Hibernate配置文件详解
    Struts工作原理、流程
    java三大框架原理
    JAVA三大框架的各自作用
    tomcat的种种
    jdk及tomcat的配置
    java-io-file
    JAVA-IO-文件过滤
    SPSS-单因素方差分析(ANOVA) 案例解析(转)
    SPSS-比较均值-独立样本T检验 案例解析(转)
  • 原文地址:https://www.cnblogs.com/cnmarkao/p/3770017.html
Copyright © 2011-2022 走看看