sql 两个表数值相加

来源:百度知道 编辑:UC知道 时间:2024/05/26 02:24:43
假设一个表A(商品名,价格)一个表B(商品名,价格)
select A.价格+B.价格
from A,B
WHERE A.商品名=B.商品名

(商品名主键,唯一)
----------------------------------
这样他就把所有的一样名称的商品价格相加了吧?

但是现在有些商品的价格不存在...在数据库为空
我用了nvl函数...还是不行...
加出来还是空....
请问怎么解决???
(说的够明白不....)

select nvl(A.价格,0)+nvl(B.价格,0)
from A full join B
where A.商品名=B.商品名
这个是oracle的
如果是其他的我也不知道好不好用
你可以
update A set 价格=0 where 价格 is NULL
update B set 价格=0 where 价格 is NULL
select A.价格+B.价格
from A,B
WHERE A.商品名=B.商品名

SQL2005测试结果
表A
name price
a 23
b 32
c 23
d 90

表B
name price
a 10
b 20
d 15

select
isnull(a.name,isnull(b.name,'无名')) as '物品名称',isnull(a.price,0)+isnull(b.price,0) as '价格和'
from a full join b
on a.name=b.name

结果
物品名称 价格和
a 33
b 52
c 23
d 105
f 14
f 14

select nvl(A.价格,0)+nvl(B.价格,0)
from A full join B
on A.商品名=B.商品名

sql server
select A.价格+B.价格 from (
select case when A.价格 is not null then A.价格 else 0 end as A.价格 ,case when B.价格 is not null then B.价格 else 0 end as B.价格
from A,B
WHERE A.商品名=B.商品名) tmp
mysql