2014-02-13 3 views
0

안녕하세요, 저는 2010 VS2008, C# 및 SQLCe 데이터베이스에서 응용 프로그램을 개발하고 있습니다.내부 조인 쿼리 잘못된 결과

소득, 지출 및 거래에 대한 세 개의 테이블에서 보고서를 생성 중입니다.

테이블 구조는 아래와 같다 :

수입 :

Income

Expence :

expence

거래 :

trans 이를 위해

나는

select t.tDate as [Date], t.tDescription as [Detail],e.eAmount as [Debit], 
i.iAmount as [Credit], t.balance as [Balance] 
from Transactions t 
    inner join Expence e on t.pid=e.pid 
    join Income i on t.pid=i.pid 
where t.pid='11' 

쿼리

을 쓴하지만 난 그냥

result

을 얻고이 쿼리를 실행할 때 나는 나의 결과는 아래와 같이 은행 문처럼해야합니다. final

내 이해에 따라 쿼리가 올바르지 않습니다.

어떻게 결과를 얻으려면 쿼리를 작성할 수 있습니까?

답변

0

결합을 사용하지 말고 left join을 사용해야합니다. left은 테이블 from에있는 모든 사례를 반환하고 Exception e Income 테이블에 대문자가 있기 때문에

select t.tDate as [Date], t.tDescription as [Detail],e.eAmount as [Debit], 
    i.iAmount as [Credit], t.balance as [Balance] 
from Transactions t 
    left join Expence e on t.pid=e.pid 
    left join Income i on t.pid=i.pid 
where t.pid='11' 
0

나는 그것이 바로 당신이 PID 및 EID를 사용해야하고 PID 및 IID를 사용해야 소득에서 항목을 가입 있도록하기 위해 트랜잭션 테이블에서 항목 Expence 테이블에서 항목을 결합 할 수있어 경우처럼 보일 것 선택 :

select t.tDate as [Date], t.tDescription as [Detail],e.eAmount as [Debit], 
i.iAmount as [Credit], t.balance as [Balance] 
from Transactions t 
    inner join Expence e on t.pid=e.pid and t.eId = e.eId 
    join Income i on t.pid=i.pid and t.iId = i.iId 
where t.pid='11'