如果数据库存储为Null,如何设置显示0

时间:2016-12-13 09:15:12

标签: sql if-statement stored-procedures

SelectionKey key = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);

对于总存款和未结清总额,我希望它显示' 0'如果在数据库中为NULL,则显示 我尝试使用if else但不工作。

CREATE PROCEDURE dbo.sp_pbos001_list AS

SELECT t1.id
    , t1.customer_code AS cus_code
    , (
        SELECT SUM(transaction_amt - cleared_amt)
        FROM pbosdeposit
        WHERE customer_code = t1.customer_code
        ) AS total_deposit
    , (
        SELECT SUM(inv_amt)
        FROM pbosinvoice
        WHERE customer_code = t1.customer_code
        ) - (
        SELECT SUM(cleared_amt)
        FROM pbosdeposit
        WHERE customer_code = t1.customer_code
        ) AS total_outstanding
FROM customer t1

3 个答案:

答案 0 :(得分:2)

您可以使用COALESCE替换NULL值:

SELECT t1.id
    , t1.customer_code AS cus_code
    ,COALESCE((
        SELECT SUM(transaction_amt - cleared_amt)
        FROM pbosdeposit
        WHERE customer_code = t1.customer_code
        ),0) AS total_deposit
    , COALESCE((
        SELECT SUM(inv_amt)
        FROM pbosinvoice
        WHERE customer_code = t1.customer_code
        ) - (
        SELECT SUM(cleared_amt)
        FROM pbosdeposit
        WHERE customer_code = t1.customer_code
        ),0) AS total_outstanding
FROM customer t1

答案 1 :(得分:1)

使用合并:

            CREATE PROCEDURE dbo.sp_pbos001_list AS

            SELECT t1.id
                , t1.customer_code AS cus_code
                , coalesce((
                    SELECT SUM(transaction_amt - cleared_amt)
                    FROM pbosdeposit
                    WHERE customer_code = t1.customer_code
                    ),0) AS total_deposit
                , coalesce(((
                    SELECT SUM(inv_amt)
                    FROM pbosinvoice
                    WHERE customer_code = t1.customer_code
                    ) - (
                    SELECT SUM(cleared_amt)
                    FROM pbosdeposit
                    WHERE customer_code = t1.customer_code
                    )),0), AS total_outstanding
            FROM customer t1

答案 2 :(得分:0)

SQL Server:

manage.py

MySQL的:

ISNULL(total_deposit, 0)