如何写if if语句?

时间:2016-06-03 09:36:01

标签: mysql

我有以下代码。 有一个是“ISNULL(PO101_EXT_COST,0)* MM01.MM021_RATE AS Amount”的一部分,“

我想更改为if else语句。 当“MM01.MM021_RATE”='1'时,PO101_EXT_COST * MM021_FOREIGN_RATE 当“MM01.MM021_RATE”不是='1'时,PO101_EXT_COST * MM021_RATE

我如何更改下面的代码?

val meaningOfLife : String? = null    
meaningOfLife?.let { println(it) } ?: println("There's no meaning")

谢谢!

1 个答案:

答案 0 :(得分:0)

/*
DROP TABLE A;
DROP TABLE B;
CREATE TEMPORARY TABLE A (ID INT, rateid int,cost int);
CREATE TEMPORARY TABLE B(ID INT,rate int,foreignrate int);
TRUNCATE TABLE A;
INSERT INTO A VALUES(1,1,1),(2,1,null),(3,2,3),(4,2,null),(5,0,null),(6,7,null) ,(7,1,null) ,(8,0,null)  ; 
TRUNCATE TABLE B;
INSERT INTO B VALUES(1,10,20),(2,30,40);
*/
#SELECT A.* FROM A;
#SELECT B.* FROM B;

SELECT  A.*,
        case 
            when B.id = 1 then
                case
                    when A.cost is not null and B.foreignrate is not null then A.cost * B.foreignrate
                    else 0
                    end
            when B.id is not null then
                case
                    when A.cost is not null and B.rate is not null then A.ID * B.rate
                    else 0
                end
            else 0
        END AS AMT
FROM        A
LEFT OUTER join         B on B.ID  = A.rateid