Sql server php mysql中的两列总和选择查询

Sql server php mysql中的两列总和选择查询,sql-server,Sql Server,我想输出0notNULL 您可以使用ISNULL()函数: select ( select ISNULL(sum(NetAmount) ,0) from Table1 where pid = '1') - ( select ISNULL(sum(NetAmount),0) from Table1 where cid = '1') select ( select

我想输出
0
not
NULL


您可以使用
ISNULL()
函数:

select ( select ISNULL(sum(NetAmount) ,0)
         from Table1 
         where pid = '1') -                 
       ( select ISNULL(sum(NetAmount),0)
         from Table1 
         where cid = '1')
select 
   ( select coalesce(sum(NetAmount), 0) 
    from Table1 
    where pid = '1') - 

    ( select coalesce(sum(NetAmount), 0) 
    from Table1 
    where cid = '1')

您可以使用
ISNULL()
函数:

select ( select ISNULL(sum(NetAmount) ,0)
         from Table1 
         where pid = '1') -                 
       ( select ISNULL(sum(NetAmount),0)
         from Table1 
         where cid = '1')
select 
   ( select coalesce(sum(NetAmount), 0) 
    from Table1 
    where pid = '1') - 

    ( select coalesce(sum(NetAmount), 0) 
    from Table1 
    where cid = '1')

对任何算术运算应用
null
将导致
null
。您可以使用
coalesce
功能解决此问题:

select ( select ISNULL(sum(NetAmount) ,0)
         from Table1 
         where pid = '1') -                 
       ( select ISNULL(sum(NetAmount),0)
         from Table1 
         where cid = '1')
select 
   ( select coalesce(sum(NetAmount), 0) 
    from Table1 
    where pid = '1') - 

    ( select coalesce(sum(NetAmount), 0) 
    from Table1 
    where cid = '1')

对任何算术运算应用
null
将导致
null
。您可以使用
coalesce
功能解决此问题:

select ( select ISNULL(sum(NetAmount) ,0)
         from Table1 
         where pid = '1') -                 
       ( select ISNULL(sum(NetAmount),0)
         from Table1 
         where cid = '1')
select 
   ( select coalesce(sum(NetAmount), 0) 
    from Table1 
    where pid = '1') - 

    ( select coalesce(sum(NetAmount), 0) 
    from Table1 
    where cid = '1')

您使用MySQL还是SQL Server?因为tag是MySQL,Fiddle是sqlserver。在MySQL中,您可以使用
IFNULL()
函数。如果您无法理解您的输出,请您简单介绍一下,您使用的是MySQL还是SQL Server?因为tag是MySQL,Fiddle是sqlserver。在MySQL中,您可以使用
IFNULL()
函数。如果您无法理解您的输出,请您向我简要介绍一下。很高兴能提供帮助