如何附加从case语句中获得的数据

时间:2015-09-22 11:40:50

标签: mysql join case

我正在使用以下代码:

select t2.lPortfolioItemId, t2.seAssignment,t3.dAcquisition as dAcquisition, 
(' Hardware Repair Level: ' || 
(CASE 
WHEN ((t3.dWarrEnd > SysDate()) OR (t5.dEnd > SysDate())) THEN 'Vendor Supported'
END 
)as 
 t3.Status as Status from (((((amPortfolio t2 left outer join amEmplDept t7 on t2.lUserId=t7.lEmplDeptID)
inner join amAsset t3 on t2.lAstId=t3.lAstId) inner join amModel t6 on t3.lModelId=t6.lModelID) left outer join amAstCntrDesc t4 on t3.lAstId=t4.lAstId) left outer join amContract t5 on t4.lCntrId=t5.lCntrId) 
where t2.lAstId<>0;

我想将我从case语句中获取的值附加到硬件修复级别字段。 我应该分配一个新变量来获取值吗?还是有其他方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

使用此

select t2.lPortfolioItemId, t2.seAssignment,t3.dAcquisition as dAcquisition, 
    CONCAT(' Hardware Repair Level: ',
        (CASE 
            WHEN ((t3.dWarrEnd > SysDate()) OR (t5.dEnd > SysDate())) THEN 'Vendor Supported'
        END 
    )) as t3.Status as Status
from 
    (((((amPortfolio t2 left outer join amEmplDept t7 on t2.lUserId=t7.lEmplDeptID)
    inner join amAsset t3 on t2.lAstId=t3.lAstId) 
    inner join amModel t6 on t3.lModelId=t6.lModelID) 
    left outer join amAstCntrDesc t4 on t3.lAstId=t4.lAstId) 
    left outer join amContract t5 on t4.lCntrId=t5.lCntrId) 
where t2.lAstId<>0;

更新

根据您的评论使用此查询

select t3.dAcquisition as dAcquisition, 
    CONCAT(     ' Contact: ', 
                t3.MaintContact, 
                ' Hardware Repair Level: ', 
                (CASE WHEN (t3.Status <> 'Disposed' OR t3.TR_PSSpares <> 1) THEN 'Vendor Not Supported' WHEN ((t3.dWarrEnd > GetDate()) OR (t5.dEnd > GetDate())) THEN 'Vendor Supported' END ) 
        ) as SupportRemarks 
from ((amPortfolio t2 left outer join amEmplDept t7 on t2.lUserId=t7.lEmplDeptID) inner join amAsset t3 on t2.lAstId=t3.lAstId) 

CONCAT是一个函数,它接受多个参数并连接所有参数。在你的情况下&#39;联系&#39;是第一个参数,t3.MaintContact是第二个参数,&#39;硬件维修等级:&#39;是第三个参数和“供应商支持”#39;来自CASE的第四个参数

您还应该检查CASE语句的正确语法。对于每个WHEN部分,您应该提供THEN部分。对于未处理的案件,ELSE部分是安全的,这是一个很好的做法,例如。

CASE WHEN a=b THEN 'both are equal' WHEN a<b THEN 'a is less than b' ELSE 'b is less than a END