MySQL Group By with Sum 删除了太多信息
谁能帮我理解 MySQL 的 Group By 是如何工作的以及我做错了什么?我正在使用一个包含两个表、拍卖表和批次的数据库。批次表有一列“auctionid”,它将每个批次链接到相关拍卖,拍卖和批次之间具有一对多关系。
我从拍品和拍卖中选择大量信息,包括使用几个 sum 和 count(distinct) 函数在每次拍卖的基础上对拍品表中的一些记录进行总计。
我的查询是这样的:
SELECT
auction.`countryId`,
auction.`date`,
auction.`name`,
auction.`awardComments`,
lots.`lotName`,
lots.`region`,
lots.`popCovered`,
SUM(lots.'size'),
SUM(lots.`paired`),
SUM(lots.`unpaired`),
COUNT(DISTINCT 'lots.winner')
FROM auction join lots USING ('awardid')
GROUP BY lots.'awardid'
“awardid”是拍卖表的主键和批次表的外键。 “lotid”是lots 表的主键,但未使用。
当我运行此查询时,每次拍卖都会得到一行,我正在寻找的是每个批次的一行,该拍卖中每个批次的拍卖信息相同,并且总和和计数函数计算总大小和数量拍品表中每次拍卖的获胜者。如果我去掉查询的“group by”和“sum”部分,那么我会得到正确的行数,每批一行。有没有办法通过 sum 和 count 函数获取我需要的所有行?基本上,我只希望将 group 子句应用于 SUM 和 COUNT 函数,如果这有意义的话 - 我希望为每次拍卖计算获胜者的数量,但显示在该拍卖中每批的结果中。
Can anyone help me understand how MySQL's Group By works and what I'm doing wrong? I'm using a database with two tables, auctions and lots. The lot table has a column for 'auctionid' which links each lot to the relevant auction, with a one:many relationship between auctions and lots.
I'm selecting a large amount of information from both the lots and auctions, including using a couple of sum and count(distinct) functions to total some records in the lots table on a per-auction basis.
My query is something like this:
SELECT
auction.`countryId`,
auction.`date`,
auction.`name`,
auction.`awardComments`,
lots.`lotName`,
lots.`region`,
lots.`popCovered`,
SUM(lots.'size'),
SUM(lots.`paired`),
SUM(lots.`unpaired`),
COUNT(DISTINCT 'lots.winner')
FROM auction join lots USING ('awardid')
GROUP BY lots.'awardid'
'awardid' is the primary key for the auction table and the foreign key for the lots table. 'lotid' is the primary key for the lots table but isn't used.
When I run this query I get one row for each auction, what I'm looking for is one row for each lot, with the auction information the same for each lot in that auction and the sum and count functions counting the total sizes and number of winners for each auction from the lots table. If I get rid of the 'group by' and 'sum' parts of the query then I get the right number of lines, one for each lot. Is there a way of getting all the lines I need with the sum and count functions? Basically I only want the group clause to apply to the SUM and COUNT functions, if that makes sense - I want the number of winners calculated for each auction, but displayed in the results for each lot in that auction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于来自同一拍卖的拍品,这确实会在每次拍卖中重复相同的总数。
This does repeat the same totals per auction for lots that come from the same auction.
我想这样的东西可以工作(警告:未经测试!):
也就是说,您在
lots
表上进行子选择来计算每次拍卖的总和,然后将其结果视为一个单独的表,您可以将其连接到结果中。I guess something like this could work (warning: untested!):
That is, you do a subselect on the
lots
table to calculate the sums per auction, and then treat the results of that as if it were a separate table which you can join to the results.