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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。