首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

先掏出PartOne符合条件的所有GUID值,再根据取出的GUID值为条件对PartTwo表执行删除操作

2012-09-27 
先取出PartOne符合条件的所有GUID值,再根据取出的GUID值为条件对PartTwo表执行删除操作。PartOne表数据如下

先取出PartOne符合条件的所有GUID值,再根据取出的GUID值为条件对PartTwo表执行删除操作。
PartOne表数据如下
GUID Name Age Time
111 aa 11 2012-08-13 20:53:00
25 bb 22 2012-08-14 20:53:00
3 cc 33 2012-08-15 20:53:00
21 rr 44 2012-09-01 20:53:00
54 qq 23 2012-09-02 20:53:00
41 i 28 2012-09-03 20:53:00


PartTwo表数据如下
GUIDTwo Name Age
55 rr 36
21 ww 42
25 xx 23
3 rw 33
111 tt 77

注:从业务角度讲,PartTwo表的GUIDTwo如果等于PartOne表中的GUID值,则表示两条数据为同一个人的数据。



我的目标:
1.将PartOne表中所有Time值 小于"2012-08-20"的GUID值取出来(也就是得到GUID的值分别为111、25、3)。
2.再以取出来GUID值为条件对PartTwo表执行删除。(也就是删除PartTwo表中 GUIDTwo分别等于111、25、3的数据)。



最终PartTwo表结果如下(也就是最后三条被删除)->

GUIDTwo Name Age
55 rr 36
21 ww 42



请教这条SQL语句如何写?(最好能一条SQL语句搞定,不过多条语句联合搞定也可以)。
多谢了。

[解决办法]
delete * from PartTwo t
where exists(select 1 from PartOne where t.GUID=GUID and Time<'2012-08-20')
[解决办法]
这题100分太赚了。
[解决办法]
试试这个:

SQL code
select GUIDTwo, Name, Agefrom PartTwoleft outer join PartOne on PartOne.GuidOne = PartTwo.GuidTwoand PartOne.Time < '2012-08-20'where PartOne.GuidOne is null
[解决办法]
SQL code
DELETE  pFROM    PartTwo AS pWHERE   EXISTS ( SELECT 1                 FROM   PartOne                 WHERE  GUID = p.GUID AND Time < '2012-08-20' )
[解决办法]
SQL code
delete from PartTowleft outer join PartOne on PartOne.Guid = PartTow.GuidTwowhere Guid is null
[解决办法]
探讨
delete * from PartTwo t
where exists(select 1 from PartOne where t.GUID=GUID and Time<'2012-08-20')

[解决办法]
SQL code
delete * from PartTwo twhere exists(select 1 from PartOne where t.GUID=GUID and Time<'2012-08-20') 

热点排行
Bad Request.