1

我正在编写一个脚本,其中拍卖应该能够包含多种产品。一个产品有一个正常价格,它是对拍卖中所有产品的总和,然后作为拍卖“包”正常价格返回。

我的问题是,我的测试拍卖的正常价格为 150 美元,但查询返回 300 美元,这意味着我在某处犯了错误。我整晚都在试图找到解决方案 - 但没有成功。有人有线索吗?

SELECT 
    a.AuctionName as A_name, 
    c.CategoryName as C_name, 
    a.AuctionEndDate as A_enddate, 
    a.AuctionOfferPrice as A_price, 
    SUM(p.ProductPrice) as A_normalprice, 
    s.StoreName as S_name 
FROM A_Auctions a 
RIGHT JOIN 
    A_Categories c 
    ON c.CategoryID = :category_id 
INNER JOIN 
    P_ProductsAuctions ppa 
    ON ppa.Auct_AuctionID = a.AuctionID 
RIGHT JOIN 
    P_Products p 
    ON p.ProductID = ppa.Prod_ProductID 
RIGHT JOIN 
    P_Attributes pa 
    ON pa.Cate_CategoryID = c.CategoryID 
RIGHT JOIN 
    P_AttributeValues pav 
    ON pa.AttributeID = pav.Attr_AttributeID 
LEFT JOIN 
    S_Stores s ON a.Stor_StoreID = s.StoreID 
WHERE 
    a.Cate_CategoryID = c.CategoryID 
    AND ((pa.InternationalName = 'param1' AND pav.AttributeValue = 'value1') OR (pa.InternationalName = 'param1' AND pav.AttributeValue = 'value2')) 
    AND ((pa.InternationalName = 'param2' AND pav.AttributeValue = 'value1') OR (pa.InternationalName = 'param1' AND pav.AttributeValue = 'value2'))
GROUP BY 
    p.ProductID, a.AuctionID, pa.AttributeID ORDER BY a.AuctionID DESC 
LIMIT 0,20

这是我的表结构:

A_拍卖:

AuctionID   int(15)         
AuctionName varchar(256)        
AuctionDescription  text        
AuctionPicture  text        
AuctionOfferPrice   decimal(15,2)           
AuctionMinimumPrice decimal(15,2)           
AuctionStartDate    datetime            
AuctionEndDate  datetime            
AuctionTimeInterval time            
AuctionNextInterval datetime            
AuctionPercentageChange decimal(5,2)            
Cate_CategoryID int(15)         
Prod_ProductID  int(15)         
Stor_StoreID    int(15)         
StUs_StoreUserID    int(15)         
date_added  datetime

P_产品:

ProductID   int(15)         
ProductName varchar(256)        
ProductDescription  text        
ProductPictureLink  longtext    
ProductPrice    decimal(25,2)           
ProductStatus   int(3)          
ProductGroupID  int(15)         
Cate_CategoryID int(15)         
StUs_UserID int(15)         
added_date  datetime

P_ProductsAuctions(存储拍卖和产品之间的 1:Many 关系):

Auct_AuctionID  int(15)         
Prod_ProductID  int(15) 

P_Attributes(产品不同变体的表):

AttributeID int(15)         
Auct_AuctionID  int(15)         
Cate_CategoryID int(15)         
AttributeName   varchar(256)        
InternationalName   varchar(256)

编辑: - 问题似乎与 P_Attributes 和 P_AttributeValues 中的参数有关,因为当我向查询中添加 1 个参数时,价格要高 150 美元 - 所以 300 美元有 1 个参数,450 美元有 2 个参数,600 美元有 3 个参数和很快...

编辑2:

在这里找到一个 SQLfiddle:http ://sqlfiddle.com/#!2/d223f/1

编辑 3:

我的输出应该是:

A_NAME -> 测试

C_NAME -> 测试

A_ENDDATE -> 2012 年 9 月 9 日 22:00:00-0400

A_PRICE -> 250

A_NORMALPRICE -> 150

S_NAME ->(空)

提前致谢,

登劳

4

1 回答 1

0

你是对的,你的属性表有问题。问题是您有多个属性,导致为每个属性返回另一行。您的 GROUP BY 组合这些行并将所有价格添加到 SUM。

如果您只是将 pav.AttributeValue 添加到您的 SELECT 和 GROUP BY 子句中,您可以看到它的实际效果,43 号为 150 美元,44 号为 150 美元。

您将不得不对其进行重构以进行子选择以获取产品 ID。我还没有完全理解你的架构,但你可能想要这样的东西:

    SELECT
  a.AuctionID ,
  c.categoryid,
  p.ProductID ,
  a.AuctionID ,
  ppa.Auct_AuctionID,


  s.storeID,

  a.AuctionName AS A_name,
  c.CategoryName AS C_name,
  a.AuctionEndDate AS A_enddate,
  a.AuctionOfferPrice AS A_price,
  sum(p.ProductPrice) as A_normalprice,
  p.ProductPrice AS A_normalprice,
  s.StoreName AS S_name
FROM
  A_Auctions a
RIGHT JOIN
  A_Categories c
  ON c.CategoryID = '1'
INNER JOIN
  P_ProductsAuctions ppa
  ON ppa.Auct_AuctionID = a.AuctionID
LEFT JOIN
  P_Products p
  ON p.ProductID = ppa.Prod_ProductID
LEFT JOIN
  P_Attributes pa
  ON pa.Cate_CategoryID = c.CategoryID
LEFT JOIN
  S_Stores s
  ON a.Stor_StoreID = s.StoreID

INNER JOIN
  (SELECT distinct Cate_CategoryID FROM P_AttributeValues pav INNER JOIN P_Attributes pa ON pa.AttributeID = pav.Attr_AttributeID 
    WHERE
      (pa.InternationalName = 'size'
      AND pav.AttributeValue = '43')
    OR
      (pa.InternationalName = 'size'
      AND pav.AttributeValue = '44')
  ) pav2  ON pav2.Cate_CategoryID = c.CategoryID 

WHERE
a.Cate_CategoryID = c.CategoryID

GROUP BY productid, auctionid, categoryid
ORDER BY
  a.AuctionID DESC
LIMIT 0,20
于 2012-09-11T15:34:06.193 回答