Mysql 查询 select 列值的总和在给定变量范围内的记录

Mysql query to select records where the sum of a column's values is in a given variable range

我一直在尝试对 select 来自 table 的所有记录编写查询,其中列值的总和介于变量范围之间。

这是我到目前为止的想法:

$result = mysql_query(' SELECT * FROM table WHERE SUM(column) BETWEEN $Range1 AND $Range2 ORDER BY RAND());

然而,当我尝试使用 mysql_fetch_object 函数遍历上述查询时,它给了我一个常见错误(提供的参数不是有效结果)。我尝试了不同的编写方式,但是仍然做空

所以我尝试使用你们提供的别名进行查询,但仍然出现相同的错误。

$result = mysql_query(' SELECT column1, SUM(column2) AS Total FROM table GROUP BY column1 HAVING Total BETWEEN $Range1 AND $Range11 ORDER BY RAND()');

我不确定 "ordering by rand" 的用途,但您的最终查询将如下所示:

SELECT *, SUM(column) AS `total`
FROM table 
GROUP BY someColumn
HAVING `total` BETWEEN $Range1 AND $Range2 
ORDER BY RAND()
;