"联合主键"使用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,转载请注明出处
posted @   kkun  阅读(3648)  评论(2编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示