SELECT id, amount FROM report
我需要 amount
成为 amount
如果 report.type='P'
和 -amount
如果 report.type='N'
.如何将其添加到上述查询中?
最佳答案
SELECT id,
IF(type = 'P', amount, amount * -1) as amount
FROM report
见 http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html .
此外,您可以在条件为空时进行处理。金额为空的情况:
SELECT id,
IF(type = 'P', IFNULL(amount,0), IFNULL(amount,0) * -1) as amount
FROM report
IFNULL(amount,0)
部分表示当amount不为null时返回amount else return 0。
关于mysql - 'SELECT' 语句中的 'IF' - 根据列值选择输出值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5951157/