gpt4 book ai didi

mysql - 'SELECT' 语句中的 'IF' - 根据列值选择输出值

转载 作者:IT老高 更新时间:2023-10-28 12:47:24 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com