gpt4 book ai didi

php - SUM 行字段 MEDOO

转载 作者:行者123 更新时间:2023-11-29 11:41:48 24 4
gpt4 key购买 nike

我正在尝试在 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 语句中的聚合函数。请改用原始查询。

试试这个:

$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

关于php - SUM 行字段 MEDOO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35669707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com