sql查询返回错误的结果

时间:2012-07-01 14:54:50

标签: mysql

我有一个mysql数据库存储报价文档,其中一些产品明确定义了每个产品的价格,还有一个合同表,用于存储合同详细信息以及它所属的客户代码和报价代码。我有以下查询,以查看在发票中写入报价的总价格是多少:

select
   sum(sqproducts.price * sqproducts.quantity) as 'total-price',
   squotations.currency as 'currency'
from
  sqproducts,
  ccontracts,
  squotations
where 
  sqproducts.contracted=1
  AND squotations.code=sqproducts.quotation_code
  AND sqproducts.quotation_code=ccontracts.squotation_code
  AND sqproducts.quotation_code='QUOT/2012/1'
group by
  currency

the following is the sqproducts table(details about the products named in quotation)

But I get this result which is not correct (it must result 1500 USD)

1 个答案:

答案 0 :(得分:0)

它是一个盲目的打击,试试这个:

select
   sum(sqproducts.price * sqproducts.quantity) as 'total-price',
   squotations.currency as 'currency'
from
  sqproducts 
inner join squotations on (squotations.code=sqproducts.quotation_code)
inner join ccontracts on (sqproducts.quotation_code=ccontracts.squotation_code)

where 
  sqproducts.contracted=1
  AND sqproducts.quotation_code='QUOT/2012/1'
group by
  squotations.currency