简体   繁体   English

SUM行字段MEDOO

[英]SUM row field MEDOO

I am trying to get a summed field in medoo. 我试图在medoo中获得一个汇总字段。

My sql at the moment is like: 我的SQL目前是这样的:

$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? 有人知道如何在medoo中发表此声明吗?

[EDIT 1] [编辑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. 这将对数据库进行更多调用,但是只要SELECT语句不支持聚合函数,它就可以正常工作。

Medoo doesn't support aggregate function in SELECT statement. MedooSELECT语句中不支持聚合函数。 Use raw query instead. 请改用raw查询。

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 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM