'IF' in 'SELECT' statement - choose output value based on column values

后端 未结 7 2155
孤城傲影
孤城傲影 2020-11-22 04:24
SELECT id, amount FROM report

I need amount to be amount if report.type=\'P\' and -amount if

7条回答
  •  孤独总比滥情好
    2020-11-22 04:51

    Use a case statement:

    select id,
        case report.type
            when 'P' then amount
            when 'N' then -amount
        end as amount
    from
        `report`
    

提交回复
热议问题