"联合主键"使用in和not in
2009-08-16 00:22
423 查看
单个字段做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,转载请注明出处
相关文章推荐
- 关于Entity Framework更新的几种方式以及可能遇到的问题(附加类型“Model”的实体失败,因为相同类型的其他实体已具有相同的主键值)在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值,则可能会发生上述行为
- 在Android M及更高版本中使用 Settings.System 抛出异常"You cannot keep your settings in the secure settings. "
- Eclipse中编译和运行时使用的JDK和JRE级别问题(关于"Bad version number in .class file"的异常解决)
- 在Android M及更高版本中使用 Settings.System 抛出异常"You cannot keep your settings in the secure settings. "
- 在Android M及更高版本中使用 Settings.System 抛出异常"You cannot keep your settings in the secure settings. "
- 在Android M及更高版本中使用 Settings.System 抛出异常"You cannot keep your settings in the secure settings. "
- 在Android M及更高版本中使用 Settings.System 抛出异常"You cannot keep your settings in the secure settings. "
- 使用dsoframer控件出现"Unable to display the inactive document. Click here to reactivate the document."的问题 .
- 附加类型“UniversalReviewSystem.Models.ApplicationUser”的实体失败,因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值
- 使用dsoframer控件出现"Unable to display the inactive document. Click here to reactivate the document."的问题
- 错误:因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 解决方法
- 在Android M及更高版本中使用 Settings.System 抛出异常"You cannot keep your settings in the secure settings. "
- 因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 。。。
- "再生龙"Clonezilla 使用技巧
- Hibernate联合主键的使用
- 解决mac OSX下安装git出现的"git命令需要使用开发者工具。您要现在安装该工具吗"
- 在DLL编程中调用模版类时出现的类似"class“XXX”需要有 dll 接口由 class“XXX”的客户端使用"的warning的解决方案
- 从一个审批需求看数据库设计——联合主键的使用
- sqlserver"无法删除数据库,因为该数据库当前正在使用"问题解决
- VC中如何使用"浏览对话框"