MySQL子查询结果,但也在其上放置标准

时间:2015-07-24 12:15:49

标签: mysql subquery

我正在尝试在严格规范化的数据库上构建查询,其中许多客户字段和值保存在不同的表中。这意味着我创建了一个子查询来返回自定义字段中的值:

SELECT
LA.ID
,GRP.GROUPNAME
,LP.PRODUCTNAME
,LA.ACCOUNTSTATE
,(LA.FEESBALANCE+LA.PENALTYBALANCE+LA.INTERESTBALANCE+LA.PRINCIPALBALANCE) AS ACCOUNTBALANCE
,AD.POSTCODE
,       (SELECT cv.`VALUE` as vl FROM customfieldvalue cv   JOIN customfield cf on cv.CUSTOMFIELDKEY = cf.ENCODEDKEY    
        WHERE LA.ENCODEDKEY = cv.PARENTKEY AND cf.ID = 'ON_Watch_List_Corporate') as 'watchstatus'
,       (SELECT cv.`VALUE` as vl FROM customfieldvalue cv   JOIN customfield cf on cv.CUSTOMFIELDKEY = cf.ENCODEDKEY    
        WHERE LA.ENCODEDKEY = cv.PARENTKEY AND cf.ID = 'ON_RI-07') as 'PROVISIONAMOUNT'
,       (SELECT cv.`VALUE` as vl FROM customfieldvalue cv   JOIN customfield cf on cv.CUSTOMFIELDKEY = cf.ENCODEDKEY    
        WHERE LA.ENCODEDKEY = cv.PARENTKEY AND cf.ID = 'ON_Security/Collateral_GV') as 'SECUITY/COLLATERAL'
,       (SELECT cv.`VALUE` as vl FROM customfieldvalue cv   JOIN customfield cf on cv.CUSTOMFIELDKEY = cf.ENCODEDKEY    
        WHERE LA.ENCODEDKEY = cv.PARENTKEY AND cf.ID = 'ON_Property_Current_Value') as 'PROPERTYVALUE'


FROM

 LOANACCOUNT LA
,LOANPRODUCT LP
,group GRP
,CUSTOMFIELD CF
,CUSTOMFIELDVALUE CV
,ADDRESS AD
,ACTIVITY AT


WHERE

LA.PRODUCTTYPEKEY = LP.ENCODEDKEY
AND LA.ACCOUNTHOLDERKEY = GRP.ENCODEDKEY
AND GRP.ENCODEDKEY = AD.PARENTKEY

但是,我还需要在WHERE标准中包含字段PROVISIONAMOUNT - 即大于0。

道歉,如果这是显而易见的,但我对SQL很新,我找不到任何显示是否可以这样做的事。

感谢能够提供帮助的任何人。

2 个答案:

答案 0 :(得分:0)

为什么不在where子句中添加条件。

WHERE
LA.PRODUCTTYPEKEY = LP.ENCODEDKEY
AND LA.ACCOUNTHOLDERKEY = GRP.ENCODEDKEY
AND GRP.ENCODEDKEY = AD.PARENTKEY
AND PROVISIONAMOUNT > 0

答案 1 :(得分:0)

看了你的查询一段时间我觉得它可以改写如下,它应该在功能上等同,但是增加了条件PROVISIONAMOUNT > 0。它应该更容易阅读并且表现更好。

在原始版本中,您已将表ACTIVITY AT包括在内,但我无法看到它在任何地方都被使用,因此我将其删除了。

这应该有用,除非我错过了一些重要的细节。请试一试,如果不起作用,我会删除我的答案。

SELECT
    LA.ID
    , GRP.GROUPNAME
    , LP.PRODUCTNAME
    , LA.ACCOUNTSTATE
    , (LA.FEESBALANCE+LA.PENALTYBALANCE+LA.INTERESTBALANCE+LA.PRINCIPALBALANCE) AS ACCOUNTBALANCE
    , AD.POSTCODE
    , MAX(CASE WHEN cf.ID = 'ON_Watch_List_Corporate' THEN cv.value END) AS 'watchstatus'
    , MAX(CASE WHEN cf.ID = 'ON_RI-07' THEN cv.value END) AS 'PROVISIONAMOUNT'
    , MAX(CASE WHEN cf.ID = 'ON_Security/Collateral_GV' THEN cv.value END) AS 'SECUITY/COLLATERAL'
    , MAX(CASE WHEN cf.ID = 'ON_Property_Current_Value' THEN cv.value END) AS 'PROPERTYVALUE'

FROM
    LOANACCOUNT LA 
JOIN 
    LOANPRODUCT LP ON LA.PRODUCTTYPEKEY = LP.ENCODEDKEY
JOIN 
    "group" GRP ON LA.ACCOUNTHOLDERKEY = GRP.ENCODEDKEY
JOIN 
    "ADDRESS" AD ON GRP.ENCODEDKEY = AD.PARENTKEY
JOIN 
    customfieldvalue cv ON LA.ENCODEDKEY = cv.PARENTKEY 
JOIN 
    customfield cf ON cv.CUSTOMFIELDKEY = cf.ENCODEDKEY 

GROUP BY
    LA.ID
    , GRP.GROUPNAME
    , LP.PRODUCTNAME
    , LA.ACCOUNTSTATE
    , (LA.FEESBALANCE+LA.PENALTYBALANCE+LA.INTERESTBALANCE+LA.PRINCIPALBALANCE) 
    , AD.POSTCODE
HAVING MAX(CASE WHEN cf.ID = 'ON_RI-07' THEN cv.value END) > 0