MySQL Group By with Sum 删除了太多信息

发布于 2024-12-08 04:27:35 字数 860 浏览 0 评论 0原文

谁能帮我理解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安穩 2024-12-15 04:27:35
SELECT
  a.countryId,
  a.`date`,
  a.name,
  a.awardComments,
  l.lotName,
  l.region,
  l.popCovered,

  sums.total_size,
  sums.total_paired,
  sums.total_unpaired,
  sums.winners

FROM auction a
INNER JOIN lots l on (a.awardid = l.awardid) 
INNER JOIN (
  SELECT 
    a2.id as auction_id
    SUM(l2.size) as total_size,
    SUM(l2.paired) as total_paired,
    SUM(l2.unpaired) as total_unpaired,
    COUNT(DISTINCT l2.winner) as winners
  FROM AUCTION a2 INNER JOIN lots l2 ON (a2.awardid = l2.awardid)
  GROUP BY a2.id
  ) sums ON (sums.auction_id = a.id) 

对于来自同一拍卖的拍品,这确实会在每次拍卖中重复相同的总数。

SELECT
  a.countryId,
  a.`date`,
  a.name,
  a.awardComments,
  l.lotName,
  l.region,
  l.popCovered,

  sums.total_size,
  sums.total_paired,
  sums.total_unpaired,
  sums.winners

FROM auction a
INNER JOIN lots l on (a.awardid = l.awardid) 
INNER JOIN (
  SELECT 
    a2.id as auction_id
    SUM(l2.size) as total_size,
    SUM(l2.paired) as total_paired,
    SUM(l2.unpaired) as total_unpaired,
    COUNT(DISTINCT l2.winner) as winners
  FROM AUCTION a2 INNER JOIN lots l2 ON (a2.awardid = l2.awardid)
  GROUP BY a2.id
  ) sums ON (sums.auction_id = a.id) 

This does repeat the same totals per auction for lots that come from the same auction.

风和你 2024-12-15 04:27:35

我想这样的东西可以工作(警告:未经测试!):

SELECT 
  auction.`countryId`,
  auction.`date`,
  auction.`name`,
  auction.`awardComments`,
  lots.`lotName`,
  lots.`region`,
  lots.`popCovered`,
  sums.`size`,
  sums.`paired`,
  sums.`unpaired`,
  sums.`winners`
FROM auction
JOIN lots USING (`awardid`)
JOIN (
  SELECT
    `awardid`,
    SUM(`size`) AS `size`,
    SUM(`paired`) AS `paired`,
    SUM(`unpaired`) AS `unpaired`,
    COUNT(DISTINCT lots.`winner`) AS `winners`
  FROM lots GROUP BY `awardid`
) AS sums USING (`awardid`)

也就是说,您在 lots 表上进行子选择来计算每次拍卖的总和,然后将其结果视为一个单独的表,您可以将其连接到结果中。

I guess something like this could work (warning: untested!):

SELECT 
  auction.`countryId`,
  auction.`date`,
  auction.`name`,
  auction.`awardComments`,
  lots.`lotName`,
  lots.`region`,
  lots.`popCovered`,
  sums.`size`,
  sums.`paired`,
  sums.`unpaired`,
  sums.`winners`
FROM auction
JOIN lots USING (`awardid`)
JOIN (
  SELECT
    `awardid`,
    SUM(`size`) AS `size`,
    SUM(`paired`) AS `paired`,
    SUM(`unpaired`) AS `unpaired`,
    COUNT(DISTINCT lots.`winner`) AS `winners`
  FROM lots GROUP BY `awardid`
) AS sums USING (`awardid`)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文