简体   繁体   English

如何让 MySQL 对几个不同行的平均字数求和?

[英]How can I make MySQL sum the average word counts of several different rows?

Basically, I have a database where entries have unique IDs, but there can be multiple versions of one thing with the same ID (differentiated by a version number that, together with the ID, forms the primary key), and those different versions may have different word counts.基本上,我有一个数据库,其中条目具有唯一的 ID,但同一 ID 可以有多个版本(由版本号区分,该版本号与 ID 一起构成主键),并且这些不同的版本可能有不同的字数。 As such, on certain pages, I'd like to display the sum of the average word count of several different entries (usually the child nodes of a given node).因此,在某些页面上,我想显示几个不同条目(通常是给定节点的子节点)的平均字数总和 The first query I tried was this:我尝试的第一个查询是:

SELECT SUM(ROUND(AVG(word_count), 0)) AS word_count FROM woh_content WHERE id IN ($children)

(Note that the $children variable is a string of comma-delimited IDs generated by a PHP function — in stripped-down versions of the above query, that part worked just fine, so I know this isn't a PHP issue.) (请注意, $children变量是由 PHP 函数生成的以逗号分隔的 ID 字符串——在上述查询的精简版本中,该部分工作得很好,所以我知道这不是 PHP 问题。)

This query didn't work, of course (although it's worth noting that several SQL syntax checkers said it was just fine), and so I started digging through Stack Overflow and the MySQL documentation to figure out what needed to be changed.当然,这个查询不起作用(尽管值得注意的是几个 SQL 语法检查器说它很好),所以我开始深入研究 Stack Overflow 和 MySQL 文档以找出需要更改的内容。 Basically everyone and everything mentioned the HAVING clause, but I couldn't find a good explanation for how exactly it would be used in a situation like this — as far as my understanding of MySQL is taking me, what I need is the WHERE clause.基本上每个人和所有事情都提到了HAVING子句,但我找不到一个很好的解释来解释它在这种情况下究竟是如何使用的——就我对 MySQL 的理解而言,我需要的是WHERE子句。 I've even used the same snippet of code for getting data on multiple IDs in other queries.我什至使用相同的代码片段来获取其他查询中多个 ID 的数据。 But, for whatever reason, that doesn't work here.但是,无论出于何种原因,这在这里都行不通。

I tried my best to come up with alternative queries using the HAVING clause instead, but none of them were... great.我尽力想出使用HAVING子句的替代查询,但它们都不是……很棒。 For example...例如...

SELECT SUM(ROUND(AVG(word_count), 0)) AS word_count FROM woh_content GROUP BY id HAVING id IN ($children)

SELECT SUM(word_count) FROM (SELECT AVG(word_count) AS word_count FROM woh_content WHERE id IN ($children) GROUP BY id) AS word_count

And, needless to say, I couldn't get any of them to work either.而且,不用说,我也无法让它们中的任何一个工作。

Anything that might help me understand this issue better would be very much appreciated.任何可以帮助我更好地理解这个问题的东西都将不胜感激。

SELECT SUM(ROUND(AVG(word_count), 0)) AS word_count FROM woh_content WHERE id IN ($children)

here of course you missed, so it will not work.这里当然你错过了,所以它不会工作。 You took AVG from the entire column at once, and it will return one scalar value to you, and you will already take the amount from one number您一次从整个列中获取了 AVG,它将返回一个标量值给您,并且您已经从一个数字中获取了金额

SELECT ROUND(AVG(word_count), 0) AS avg, SUM(avg) FROM woh_content
    WHERE id IN ($children)
    GROUP BY word_count 


SELECT SUM(ROUND(AVG(word_count), 0)) AS word_count FROM woh_content
    WHERE id IN ($children)
    GROUP BY word_count 

Wrote in a hurry, it would be nice to check, because.写的很匆忙,检查一下就好了,因为。 I can be wrong.我可能是错的。 But the bottom line is that first you have to ungroup your records, and the avg function will be the aggregate function for merging identical records on the column specified in group by .但最重要的是,首先您必须取消对记录的分组,并且 avg 函数将是聚合函数,用于合并 group by 中指定的列上的相同记录。 But according to your question and example, it's still not clear what you wanted.但是根据您的问题和示例,仍然不清楚您想要什么。 Because according to the explanation in the group by should be the first half of the composite index, which is directly repeated因为根据group by里的解释应该是综合指数的前半部分,直接重复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM