为什么列值的总和 return 1?

Why SUM of column values return 1?

我正在尝试获取用户的总和 当月的访问量。这是我的代码(如果重要的话,我使用 ORM RedBeanPHP):

$summ = R::exec('SELECT SUM(amount) FROM statistic WHERE type = "usuall_visit" AND month = :month',[':month'=>date('m')]);

并且 $summ 始终为 1,即使它不是真的。 ._. 我的 table 统计数据: https://drive.google.com/open?id=1MFt-FtEtqkk7QWQC2oAtMBA0WAgOIV4h

P.S。此外,当我尝试仅获取具有我需要的列的最大值的行的值时,如下所示:

$summ = R::exec('SELECT MAX(amount),value FROM statistic WHERE type = "usuall_visit" AND month = :month',[':month'=>date('m')]);

我运行进入错误: "Syntax error or access violation: 1140 In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'somesite.statistic.value'; this is incompatible with sql_mode=only_full_group_by" 我说好的,然后尝试添加 GROUP BY ID。但后来我只得到第一行,但不仅有实际最大值._。我不知道我做错了什么

在 RedBeanPHP 中,对于 SELECT 查询,您必须使用 R::exec 以外的其他函数。您得到的数字可能是 select 返回的行数或指示成功的简单值。

尝试

$summ = (int)R::getCell('SELECT SUM(amount) FROM statistic WHERE type = "usuall_visit" AND month = :month',[':month'=>date('m')]);

对于第二个问题,您可能想研究一下 SQL,至少是最基础的。像这样的嵌套查询可能适合您。

SELECT value FROM statistic WHERE amount = (SELECT MAX(amount) FROM statistic WHERE type = "usuall_visit" AND month = :month') LIMIT 1

您将需要再次将其与 R::getCell 一起使用(如果您想检索多列,则需要 R::getRow)。