求一SQL语句(50)

  • 主题发起人 主题发起人 sjm
  • 开始时间 开始时间
S

sjm

Unregistered / Unconfirmed
GUEST, unregistred user!
Select a.ID, b.Name, a.[Time] from Origin a join Cards b on CardID = PatrolMan where (b.Name = '丁彬' or b.Name = '刘雪梅') and a.[Time]>='2009-4-3' and a.[Time]<'2009-4-4' order by b.Name以上语句可以正常查询。现在我想在查询的结果集中删除a.ID = 1234的记录,我用了以下写法但报错:Delete from(Select a.ID, b.Name, a.[Time] from Origin a join Cards b on CardID = PatrolMan where (b.Name = '丁彬' or b.Name = '刘雪梅') and a.[Time]>='2009-4-3' and a.[Time]<'2009-4-4' ) where a.ID = 1234请问应该如何修改才能通过呢?谢谢!
 
用having怎么样Select a.ID, b.Name, a.[Time] from Origin a join Cards b on CardID = PatrolMan where (b.Name = '丁彬' or b.Name = '刘雪梅') and a.[Time]>='2009-4-3' and a.[Time]<'2009-4-4' having(a.id<>1234) order by b.Name
 
--加上a.ID<>'1234'作为查询条件就可以了啊Select a.ID, b.Name, a.[Time] from Origin a join Cards b on CardID = PatrolMan where (b.Name = '丁彬' or b.Name = '刘雪梅') and a.[Time]>='2009-4-3' and a.[Time]<'2009-4-4' and a.ID<>'1234' order by b.Name
 
组合查询是没办法删记录的。你可以把上来的查询语句做成一个视图(View), 再从视图里删除试试。
 
做成视图试了一下也不行,不允许删除的。
 
sql server删除其中一个表的方法: //如果BB_Temp中一条记录对应数据集P中多条记录,该语句仍然可以正确执行,但Access不可以。 Delete bb_Temp from (bb_temp left join (select c0,count(*) as C2 from bb_param group by C0) P on bb_temp.c0=P.c0 ) where p.c2>0;----------------------------------Access删除多个表的方法 Delete BB_Temp.*,BB_Param.* from ( bb_temp inner join (select c0,C1 from bb_param ) P on bb_temp.c0=P.c0);--------------sql server一个语句同时删除多个表的方法,没有试出来
 
或者:Select a.主键 key_a, b.主键 key_b into #TmpTablefrom Origin a join Cards b on CardID = PatrolMan where (b.Name = '丁彬' or b.Name = '刘雪梅') and a.[Time]>='2009-4-3' and a.[Time]<'2009-4-4' order by b.Name;Delete from Origin where 主键 in (Select key_a from #TmpTable);Delete from Cards where 主键 in (Select key_b from #TmpTable);drop table #TmpTable;
 
后退
顶部