在SQL Server中尝试使用内部联接获取数据时,列获取错误数据
我的数据库中有两个表在SQL Server中尝试使用内部联接获取数据时,列获取错误数据,sql,sql-server-2012,Sql,Sql Server 2012,我的数据库中有两个表 交易表 M_Trans Table 我已经这样设置了查询 select distinct inv.transId as Order ID, sfc.nofaktur as Faktur, inv.amount as PPn, inv.salesId from Transaction inv inner join M_Trans sfc on sfc.invoice=inv.invoice where ( DATEPART(yy, inv.d
select distinct inv.transId as Order ID,
sfc.nofaktur as Faktur,
inv.amount as PPn,
inv.salesId
from Transaction inv
inner join M_Trans sfc on sfc.invoice=inv.invoice
where (
DATEPART(yy, inv.date) = '2018'
AND DATEPART(mm, inv.date) = '2'
)
order by sfc.nofaktur asc
但它不能满足我的需要,我的超越结果如下:`
查询结果如下:
(salesId中的数据不正确)
如何修复查询以便获取所需数据?
谢谢MySQL不是Microsoft SQL server。您到底使用了什么?
内部连接M_Trans efa在sfc.invoice=inv.invoice
-什么是sfc
?此查询如何工作?表中没有一个别名是sfc
,但您正在使用它sfc.invoice
很抱歉,我已经更新了问题。我使用的是基于您提供的示例数据的SQL Server(没有inv.date
,因此我没有使用WHERE
子句),您的查询工作正常。