日期:2014-05-18  浏览次数:20450 次

4个表取数据。。。求帮忙解决。。。
先说下这4张表:

Provider: ProId(供应商Id、主键)、Name(供应商名字)

GoodsInfo: GoodsId(商品id、主键)、Name(商品名称)、GuiGe(规格)、Unit(产地)、ProId(供应商Id、外键)

BuyInfo: BuyId(订单编号、主键)、ProId(供应商Id、外键)、DateOfBuy(采购日期、datetime类型)

BuyDetails: BuyId(订单编号、外键)、GoodsId(商品id、外键)、Count(数量)



我想搜索出GoodsInfo.GoodsId、GoodsInfo.Name、GuiGe、Unit、Provider.Name、sum(BuyDetails.count) 这6列的数据,条件是在2个时间段内,就是BuyInfo表里面的DateOfBuy,我自己尝试写了SQL语句,不过报错了。。。报的错误是:无法绑定由多个部分组成的标识符 "Provider.ProId"和无法绑定由多个部分组成的标识符 "Provider.Name"。

我自己写的SQL语句:
select GoodsInfo.GoodsId,GoodsInfo.Name,Locality,Unit,GuiGe,Provider.Name,sum(BuyInfo.count) from GoodsInfo,ProviderInfo,BuyInfo,BuyDetails where GoodsInfo.GoodsId='"+GoodsId+"' and GoodsInfo.GoodsId=BuyDetails.GoodsId and Provider.ProId=GoodsInfo.ProId and BuyInfo.BuyId=BuyDetails.BuyId and BuyInfo.DateOfBuy>=@tm1 and BuyInfo.DateofBuy<=@tm2

------解决方案--------------------
SQL code
select
  b.GoodsId,b..Name,b.GuiGe,b.Unit,a.name,sum(d.[count])
from
  Provider a,GoodsInfo b,BuyInfo c,BuyDetails d
where 
  b.GoodsId='xx'
and  
  b.GoodsId=d.GoodsId
and
  a.ProId=c.ProId
and
  c.BuyId=d.BuyId
and
  c.DateOfBuy>=@tm1 and c.DateofBuy<=@tm2
group by
  b.GoodsId,b..Name,b.GuiGe,b.Unit,a.name