日期:2014-05-17  浏览次数:20530 次

先取出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, Age
from PartTwo
left outer join PartOne on PartOne.GuidOne = PartTwo.GuidTwo
and PartOne.Time < '2012-08-20'
where PartOne.GuidOne is null

------解决方案--------------------
SQL code
DELETE  p
FROM    PartTwo AS p
WHERE   EXISTS ( SELECT 1
                 FROM   PartOne
                 WHERE  GUID = p.GUID AND Time < '2012-08-20' )

------解决方案--------------------
SQL code


delete from PartTow
left outer join PartOne on PartOne.Guid = PartTow.GuidTwo
where 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 t
where exists(select 1 from PartOne where t.GUID=GUID and Time<'2012-08-20')