du1913 2016-02-27 12:21
浏览 99
已采纳

SUM行字段MEDOO

I am trying to get a summed field in medoo.

My sql at the moment is like:

$database->debug()->select("user_rupees_in_house", [
    "[<]rupees" => ["rupee_id" => "ID"]
], [
    "name",
    "amount",
    "amount_spend"
], [
    "user_uuid"  => $user,
    "GROUP" => "rupee_id"
]);

The debug logs the following statement:

SELECT `name`,`amount`,`amount_spend`, `rupee_id`
FROM `user_rupees_in_house` 
RIGHT JOIN `rupees` 
ON `user_rupees_in_house`.`rupee_id` = `rupees`.`ID` 
WHERE `user_uuid` = '4da9ff11-56ca3a2f-b3ab-a25b9230'
GROUP BY `rupee_id`

What I'm trying to achieve is:

SELECT `name`,SUM(`amount`),SUM(`amount_spend`), `rupee_id`
FROM `user_rupees_in_house` 
RIGHT JOIN `rupees` 
ON `user_rupees_in_house`.`rupee_id` = `rupees`.`ID` 
WHERE `user_uuid` = '4da9ff11-56ca3a2f-b3ab-a25b9230'
GROUP BY `rupee_id`

Does anybody know how to make this statement in medoo?

[EDIT 1]

Found another way of achieving this

// Get the rupee types
$rupee_types = $database->select("rupees", "ID");

foreach ($rupee_types as $rupee_type) {
    $amount = $database->sum("user_rupees_in_house", "amount", [
        "AND" => [
            "rupee_id" => $rupee_type,
            "user_uuid" => $user
        ]
    ]);

    // Build array of rupees
}

This will make a lot more calls to the database, but is working just fine as long as the SELECT statement does not support aggregate functions.

  • 写回答

1条回答 默认 最新

  • douxiajia6104 2016-02-27 12:32
    关注

    Medoo doesn't support aggregate function in SELECT statement. Use raw query instead.

    Try this:

    $result = $database->query(
        "SELECT `name`,SUM(`amount`),SUM(`amount_spend`), `rupee_id`
        FROM `user_rupees_in_house` 
        RIGHT JOIN `rupees` 
        ON `user_rupees_in_house`.`rupee_id` = `rupees`.`ID` 
        WHERE `user_uuid` = '$user'
        GROUP BY `rupee_id`"
    )->fetchAll();
    

    Reference

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

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable