gpt4 book ai didi

Mysql 加入 - 返回两倍于预期的数字

转载 作者:行者123 更新时间:2023-11-30 23:27:46 25 4
gpt4 key购买 nike

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

我的问题是,我的测试拍卖的正常价格为 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_Auctions:

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 美元 - 因此 1 个参数 300 美元,2 个参数 450 美元,3 个参数 600 美元等等...

编辑 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 ->(空)

提前致谢

登劳。

最佳答案

您是对的,您的属性表存在问题。问题是您有多个属性导致为每个属性返回另一行。您的 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

关于Mysql 加入 - 返回两倍于预期的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12328539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com