'SELECT'语句中的“IF”

编程入门 行业动态 更新时间:2024-10-21 16:22:41
本文介绍了'SELECT'语句中的“IF” - 根据列值选择输出值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 SELECT id, amount FROM report

code> amount 为 amount if report.type ='P'和 -amount 如果 report.type ='N'。如何将此添加到上述查询中?

I need amount to be amount if report.type='P' and -amount if report.type='N'. How do I add this to the above query?

推荐答案

SELECT id, IF(type = 'P', amount, amount * -1) as amount FROM report

请参阅 dev.mysql /doc/refman/5.0/en/control-flow-functions.html 。

此外,您可以在条件为null时处理。在空值的情况下:

Additionally, you could handle when the condition is null. In the case of a null amount:

SELECT id, IF(type = 'P', IFNULL(amount,0), IFNULL(amount,0) * -1) as amount FROM report

IFNULL(amount,0)表示当金额不为null时返回金额为其他回报0 。

更多推荐

'SELECT'语句中的“IF”

本文发布于:2023-10-16 16:26:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   SELECT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!