所以这是我的MySQL表的结构,我想用它来完成:
ID type category_id amount
12 Expense 3 963.39
13 Expense 5 1200.50
14 Expense 3 444.12
15 Expense 5 1137.56
..............................
期望的输出:
1407,41 (for category_id = 3)
2338,06 (for category_id = 5)
....... (and for other category_id)
我现在得到的是什么:
1407,41 (only for category_id = 3)
我的查询不会添加或显示其他category_id的总和。
这是我正在尝试的查询:
$query = "SELECT SUM(amount) AS TotalAmount FROM spendee WHERE type = 'Expense'
group by category_id having count(*) >1 ";
$expense_query = mysqli_query($connection, $query);
$expense_count = mysqli_fetch_array($expense_query);
echo $expense_count[0];
过去几天一直坚持这一点。很感谢任何形式的帮助。谢谢!
答案 0 :(得分:0)
您只需拨打mysqli_fetch_array()
一次。您需要在循环中调用它以获取所有总计。您还应该在SELECT
列表中包含类别ID。
$query = "SELECT category_id, SUM(amount) AS TotalAmount FROM spendee WHERE type = 'Expense'
group by category_id having count(*) >1 ";
$expense_query = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($expense_query)) {
echo "{$row['TotalAmount']} (for category_id = {$row['category_id']}<br>\n";
}
答案 1 :(得分:-1)
此处的查询有效。只是你只选择$ expense_count变量的第一个结果。 $ expense_count [1]将返回列出的第二个类别,$ expense_count [2将返回第三个类别,等等...
试试function createNetworkResource (resourceName, inputDirectory) {
request.post({
url: browser.params.baseRestUrl + 'resources/connections',
auth: browser.params.auth,
json: true,
body: {
displayName: "RESOURCE_TEST",
name: "RESOURCE_TEST",
type: "NETWORK"
}
},
function(error, response, user){
if (error) return console.error('error(%s):', resourceName, error)
console.log("Status code of createNetworkResource(%s): %s", resourceName, response.statusCode)
}
);
};
度过愉快的一天。