0

我有以下查询:

select acc.Username from accounts acc
left join accountevents ace on acc.accountid=ace.accountid
left join leads lds on lds.leadid=acc.leadid
where lds.DateEntered>'2011-01-01' and lds.DateEntered<'2011-02-01'
Group by acc.Username
Having count(ace.accounteventid)=0

我想让它成为一个删除语句:

delete username from accounts 
left join accountevents ace on accounts.accountid=ace.accountid 
left join leads lds on lds.leadid=accounts.leadid
where Leads.DateEntered>'2011-01-01' and lds.DateEntered<'2011-02-01' 
Having count(accountevents.accounteventid)=0

我收到以下错误:关键字“Having”附近的语法不正确。

有什么建议么?

4

2 回答 2

5

我认为您应该按照以下方式重写它:

delete from accounts where username in ( <here goes your select statement> );
于 2012-07-12T13:13:00.987 回答
0

从选择查询中可以看出,表accounts 与表accountevents 和leads 有关系。所以首先你必须从子表或外键表和主表中删除记录。如果您DELETE Rule to CASCADE在数据库设计或表中设置了 ,请运行此查询。

DELETE FROM [TableName] where UserName ='UserName'   -- TableName should be your primary table it may be Accounts or AccountsEvents or Leads

如果您没有在数据库设计中设置删除规则,那么您必须运行三个查询。首先从子表中删除数据,然后从父表中删除数据。

Delete from accounts where username = 'name'
Delete from eventsaccounts where accountid = 'accountsid'
Delete from leads where leadid = 'leadid'
于 2012-07-12T13:39:49.163 回答