2012-07-12 2 views
0

나는 다음과 같은 쿼리가 : 내가 원하는SQL 선택 쿼리

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'.

제안 사항?

+2

** ** 데이터베이스 시스템 및 버전은 무엇입니까 ?? * SQL *은 많은 데이터베이스 시스템에서 사용되는 언어이지만 구조적 쿼리 언어 * 일뿐입니다.이 같은 기능은 벤더에 따라 달라질 수 있습니다. 그래서 우리는 실제로 데이터베이스 시스템 **을 알아야합니다. ** 당신은 ....를 사용하고 있습니다. –

+0

삭제시 GROUP BY 절을 꺼 냈습니다. HAVING을 사용하려면 GROUP BY가 필요합니다. 그렇지 않으면'AND COUNT (Blah) = 0'을 사용하고, count는 ** 필요하지 않습니다. ** GROUP BY – Charleh

답변

5

나는 당신이 그것을 다음과 같은 방법을 다시 작성해야한다고 생각 :

delete from accounts where username in (<here goes your select statement>); 
0

를 선택 쿼리는 테이블 계정이 테이블 accountevents와 리드와 관계를 가지고 분명하다. 먼저 자식 테이블이나 외래 키 테이블에서 레코드를 삭제하고 기본 테이블에서 레코드를 삭제해야합니다. 데이터베이스 디자인이나 테이블에 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 규칙을 설정하지 않은 경우 세 가지 쿼리를 실행해야합니다. 먼저 하위 테이블에서 데이터를 삭제 한 다음 부모에서 삭제합니다.

Delete from accounts where username = 'name' 
Delete from eventsaccounts where accountid = 'accountsid' 
Delete from leads where leadid = 'leadid'