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,转载请注明出处
  • 相关阅读:
    Mysql外键和表关系
    列类型-字符类型
    列类型-日期时间型
    列类型
    破解MySQL的root密码
    数据库简单使用
    数据库简介
    socketserver
    python网络编程-粘包问题的解决
    python-网络编程,简单模型
  • 原文地址:https://www.cnblogs.com/kkun/p/1546990.html
Copyright © 2011-2022 走看看