zoukankan      html  css  js  c++  java
  • "联合主键"使用in和not in

    单个字段做in和not in操作非常方便(虽然效率不高)

    但是如果没有这样的单个字段,必须需要两个或两个以上的字段才能匹配的时候,还能不能用in和not in呢?

    类似没有唯一标识的主键,但是有联合主键,由于工作中遇到了这种情况而之前又没这么干过,刚刚想起来写脚本做测试

    结果是:当然可以!

    高手请多指教,代码如下:

    /*---------------------------------华丽的分隔线.Begin----------------------------------*/
    use master
    go
    create database test
    go
    use test
    go
    create table t1(
        i1 int,
        i2 int,
        i3 int
    )
    create table t2(
        i1 int,
        i2 int,
        i3 int,
        i4 int
    )
    
    insert into t1
    select 1,2,1 union all
    select 1,2,2 union all
    select 1,2,3 union all
    select 1,2,4 union all
    select 1,2,5 
    
    insert into t2
    select 1,2,3,1 union all
    select 1,2,3,2
    
    /*-------------------------
    删除t1表中i+i3等于t2表中i1+i4的行
    -------------------------*/
    delete from t1
    where i1 + i3 in (select i1 + i4 from t2)
    
    /*-------------------------
    将t2表中i1+i4不等于t1表中i1+i3的记录添加至t2表中
    -------------------------*/
    insert into t2
    select i1,i2,i3,0 from t1
    where i1+i3 not in (select i1 + i4 from t2)
    
    /*-------------------------
    drop table & database
    -------------------------*/
    drop table t1
    drop table t2
    
    use master
    go
    drop database test
    /*---------------------------------华丽的分隔线.End---------------------------------*/
    记录学习中的点点滴滴,记录这一路走来的风景
    文章来自http://cnblogs.com/kkun,转载请注明出处
  • 相关阅读:
    144环形链表
    83. 删除排序链表中的重复元素
    21合并两个有序链表
    PyCharm2020激活破解教程
    Python正课目录
    2条pip命令解决Python项目依赖的导出和导出
    pip离线安装模块
    Python正课149 —— luffy项目 User表的配置
    Python正课148 —— luffy项目 数据库配置
    解决:django中LookupError No installed app with label 'admin'
  • 原文地址:https://www.cnblogs.com/kkun/p/1546990.html
Copyright © 2011-2022 走看看