dshyu6866 2019-05-26 15:47
浏览 67
已采纳

为什么列值的SUM返回1?

I m trying to get sum of user's visits for current month. This is my code (if important to know,I use ORM RedBeanPHP):

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

And $summ is always 1, even if its not true. ._. My table statistic: https://drive.google.com/open?id=1MFt-FtEtqkk7QWQC2oAtMBA0WAgOIV4h

P.S. Also, when I try get values only of row with max value of column I need,like this:

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

I run into error: "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" I said ok, and tried add GROUP BY ID. But then I get just 1st row, but not only with actuall max value ._. I dunno what I did wrong

  • 写回答

1条回答 默认 最新

  • dongtui2029 2019-05-26 16:51
    关注

    In RedBeanPHP, you must use other function than R::exec for SELECT queries. The number you are getting is probably number of rows returned by select or a simple value indicating success.

    Try

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

    For the second question, you might want to study SQL a bit, at least the very basics. Nested query like this may work for you.

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

    You will need to use it with R::getCell again (or R::getRow if you wanted to retrieve multiple columns).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配