主题:sql中如何让null+10=10而不是null
标题是sql语句的症结所在,产生该症结的问题所在如下:
比如有
父表
td1
id(主键) td1_num 字段
1 20 值
2 15
3 40
子表(用id和父表联系)
td2
id td2_num 字段
1 4
1 5
2 3
自己写了条sql语句:
select (select sum(td2_num) from td2 as a where a.id=b.id)+b.td1_num) as num from td1 as b
就是统计td2_num的值在加上td1里面td1_num的值构成一个新字段。
但显示结果如下:
num
29 (5+4+20)
18 (3+15)
NUll 这个结果不是我想要的,因为null(因为没该id的值所以为NUll)+40 这里我想要是让他=40而不是null.
头痛啊,想好久不知道怎么办。高手帮忙下。
比如有
父表
td1
id(主键) td1_num 字段
1 20 值
2 15
3 40
子表(用id和父表联系)
td2
id td2_num 字段
1 4
1 5
2 3
自己写了条sql语句:
select (select sum(td2_num) from td2 as a where a.id=b.id)+b.td1_num) as num from td1 as b
就是统计td2_num的值在加上td1里面td1_num的值构成一个新字段。
但显示结果如下:
num
29 (5+4+20)
18 (3+15)
NUll 这个结果不是我想要的,因为null(因为没该id的值所以为NUll)+40 这里我想要是让他=40而不是null.
头痛啊,想好久不知道怎么办。高手帮忙下。