我想在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
语句不支持聚合函数,它就可以正常工作。
答案 0 :(得分: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();