SQL IN语句没有正确检查?

时间:2016-03-07 16:17:12

标签: sql odbc razorsql

我正在使用具有多个"代码的表中的列/字段"或者" ids"与之相关。

afacctbal 有一个名为 afacbbalid 的列/字段,其中可能包括COST,PRN或COLL。

我遇到的问题是在账户上提取COST等于0的行。

我在where子句中尝试了一个子查询的SQL语句,但返回了一般错误。现在我正在尝试使用IN语句,但这似乎也不起作用。

以下是我所得到的:

select araccount.aracid as AccountID, arentity.arenst State, arentity.ARENNAME ClientName,
 afaccount.afaccurbal CurrentBal, afacctbal.afacbcurbal PrincipalBal,
  afacctbal.AFACBbalid
from araccount
 inner join arrelationship on araccount.aracid = arrelationship.arrelacid
 inner join arentity on arentity.arenid = arrelationship.ARRELENID
 inner join afaccount on afaccount.afacacctid = araccount.ARACID
 inner join afacctbal on afaccount.AFACKEY = afacctbal.AFACBACCTID
where afacctbal.afacbbalid in("COST",0)
    and afaccount.AFACRATEID = "MN100"
    and arentity.ARENST = "MN"
    and araccount.araclstdte > "2013-04-01"
order by afaccount.afaccurbal

麻烦在于我的where子句 这里:

where afacctbal.afacbbalid in("COST",0)

我如何检查COST并检查它是否等于0?

AFACCTBAL TABLLE 使用价值:  afacbbalid - balance id  afacbcurbal - 每个余额的余额。 afaccount表 使用价值:  afaccurbal - 当前余额

2 个答案:

答案 0 :(得分:0)

您使用的是"COST"字符串,然后是0这是一个整数。因此,您需要在afacctbal.afacbbalid列中的数据类型中转换它们。

答案 1 :(得分:0)

我认为以下查询将符合您的要求 -

select araccount.aracid as AccountID, arentity.arenst State, arentity.ARENNAME ClientName,
 afaccount.afaccurbal CurrentBal, afacctbal.afacbcurbal PrincipalBal,
  afacctbal.AFACBbalid
from araccount
 inner join arrelationship on araccount.aracid = arrelationship.arrelacid
 inner join arentity on arentity.arenid = arrelationship.ARRELENID
 inner join afaccount on afaccount.afacacctid = araccount.ARACID
 inner join afacctbal on afaccount.AFACKEY = afacctbal.AFACBACCTID
where ((afacctbal.afacbbalid ='COST'
    and afaccurbal.currentBal = 0) or 
     (afacctbal.afacbbalid ='PRN'
    and afaccurbal.currentBal > 0))
    and afaccount.AFACRATEID = "MN100"
    and arentity.ARENST = "MN"
    and araccount.araclstdte > "2013-04-01"
order by afaccount.afaccurbal