简体   繁体   English

在MySQL中有多个条件的情况

[英]case when with multiple conditions in mysql

CASE WHEN (D.BUYQ>0 AND D.SELLQ>0 AND D.series_expiry='2050-01-01') then
 sum(D.stt_INTRA_buy*least(D.buyq,D.SELLQ)*D.buyavg)+sum(D.stt_buy*(D.buyq-least(D.buyq,D.SELLQ))*D.buyavg) 
ELSE
sum(D.stt_buy*D.buyq*D.buyavg) 
 END) as xsttbuy

The case condition always held's False and it enters into else even after data being there in the database which satisfies the True condition. 案例条件始终保持为False,即使数据库中存在满足True条件的数据,它也会进入其他条件。

I suspect you mean to be applying your test to each database row, not to each result row. 我怀疑您的意思是将测试应用于每个数据库行,而不是每个结果行。 If so, you need to move the case into the sum: 如果是这样,则需要将个案移到总和中:

SUM(
    CASE WHEN (D.BUYQ>0 AND D.SELLQ>0 AND D.series_expiry='2050-01-01') THEN
        D.stt_INTRA_buy*LEAST(D.buyq,D.SELLQ)*D.buyavg)+D.stt_buy*(D.buyq-LEAST(D.buyq,D.SELLQ))*D.buyavg 
    ELSE
        D.stt_buy*D.buyq*D.buyavg
    END
) AS xsttbuy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM