SUM 行字段 MEDOO

SUM row field MEDOO

我正在尝试在 medoo 中获取求和字段。

我现在的 sql 是这样的:

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

调试记录以下语句:

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`

我想要实现的是:

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`

有人知道如何在 medoo 中做这个声明吗?

[编辑 1]

找到另一种实现方法

// 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
}

这将对数据库进行更多调用,但只要 SELECT 语句不支持聚合函数就可以正常工作。

Medoo 不支持 SELECT 语句中的聚合函数。请改用 raw 查询。

试试这个:

$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