这条sql语句该怎么写?(0分)

  • 主题发起人 主题发起人 deron
  • 开始时间 开始时间
D

deron

Unregistered / Unconfirmed
GUEST, unregistred user!
两个表product.db(a) ,inventory.db(b)
要求显示表a中的所有记录,如果b表中有该种产品的库存,则显示库存数,
如果没有,则显示库存数为0。
两表中有‘产品编码’关联字段。
 
select * ,(Select sum(库存) from b where b.ProductID=a.ProductID) as 库存 from a
没分
 
Select a.* ,b.库存数
FROM a left join b on a.产品编码=b.产品编码
建议使用 left join 连接
 
谢谢两位无私的奉献!等以后有分了再行感谢。
 
HuangShuiYuan 的很OK!,Thank you very much!
lodgue的好像没有结果显示?不知是否跟数据库种类有关?
 
lodgue的好象是临时统计库存数的,是当你的表inventory.db(b)中没有库存数字段的时候……
 
sql 7.0中:
建立a:
create Table a
(
productID char(2),
productName char(2)
)
create Table b
(
productID char(2),
productAmount int
)

输入数据 a: 1 ,1
2, 2
3, 3
4, 4
b:
1,1
1,1
2,2
2,2
3,3
查询:
Select * ,
(Select sum(b.ProductAmount)+0 from b where b.ProductID = a.ProductId) as 库存
from a
结果:
ProductID productName 库存
--------- ----------- -----------
1 1 2
2 2 4
3 3 3
4 4 NULL
 
谢谢各位了!!
 

Similar threads

回复
0
查看
830
不得闲
S
回复
0
查看
857
SUNSTONE的Delphi笔记
S
S
回复
0
查看
783
SUNSTONE的Delphi笔记
S
D
回复
0
查看
848
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部