C# 内部联接查询结果不正确
您好,我正在VS2010、C#和SQLCe数据库中开发一个应用程序 我正在生成一份来自3个表的报告,分别是收入、支出和交易 表结构如下: 收入: 费用: 交易: 为此,我编写了一个查询C# 内部联接查询结果不正确,c#,sql,join,sql-server-ce,inner-join,C#,Sql,Join,Sql Server Ce,Inner Join,您好,我正在VS2010、C#和SQLCe数据库中开发一个应用程序 我正在生成一份来自3个表的报告,分别是收入、支出和交易 表结构如下: 收入: 费用: 交易: 为此,我编写了一个查询 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 j
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'
但是当我运行这个查询时,我只得到了
我希望我的结果应该像下面的银行对账单一样。
根据我的理解,这个问题是不对的
我们如何编写查询以获得这样的结果?您必须使用
左连接,而不是使用连接。因为left
返回表from
中的所有案例,并且案例存在于表Expence 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'
如果我正确地将Expence表中的项目与Transactions表中的项目连接起来,您应该使用pId和eId,将Income表中的项目连接起来,您应该使用pId和iId,因此您的选择如下所示:
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'