Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 内部联接查询结果不正确_C#_Sql_Join_Sql Server Ce_Inner Join - Fatal编程技术网

C# 内部联接查询结果不正确

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

您好,我正在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 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'