mysql - SQL 案例语句

标签 mysql sql

我有一个名为“allocation”的表,其中的列名为“all_code”、“grs_amt”和“cut_amt”。我想创建一个 SQL 语句来获取 Gross_Value、Cut_Value 和 Net_Value 等结果。如果“cut_amt”为空,则为 Cut_Value 分配 0,然后使用分配的 0 值或“cut_amt”中的可用值计算 Net_Value。我使用了以下查询。

SELECT allocation.grs_amt AS Gross_Value,
  IfNull(allocation.cut_amt, 0) AS Cut_Value,
  allocation.grs_amt - allocation.cut_amt AS Net_Value FROM
  allocation 

02) 但无法获得所需的输出。无法理解我出了什么问题。谁能帮我吗?

最佳答案

你必须重复这个表达式:

SELECT a.grs_amt AS Gross_Value,
       coalesce(a.cut_amt, 0) AS Cut_Value,
       (a.grs_amt - coalesce(a.cut_amt, 0)) AS Net_Value
FROM allocation a;

在大多数情况下我更喜欢 coalesce(),因为它是 ANSI 标准。

关于mysql - SQL 案例语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40706293/

相关文章:

mysql - 如何在MySQL中的多个参数表中进行搜索?

MySQL 事务不停止 for 循环中的竞争条件

View 中的 SQL CTE 与存储过程中的临时表

sql - 更新错误 : Receiving Error ORA - 01427 Single-row subquery returns more than one row

sql - 在没有大型自连接的情况下查找具有不匹配值的行集?

sql - 在 postgreSQL 中减去 bigint 时间戳但没有得到确切的答案

php - MySQL CONCAT_WS 三列,在explode()之后在foreach循环中回显结果

MySQL根据同表中其他列的总和更新语句

php - 如何在收件箱中获取最后一条消息?

sql - 将记录的每一列转换为单独的记录