0

我正在尝试在 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语句不支持聚合函数,它就可以正常工作。

4

1 回答 1

1

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();

参考

于 2016-02-27T12:32:19.310 回答