gpt4 book ai didi

javascript - 带有 EJS 的 Node.js Express 项目中的 MySQL SUM 问题

转载 作者:行者123 更新时间:2023-11-30 11:04:54 31 4
gpt4 key购买 nike

我目前正在尝试在我的 Node.js 项目上构建一个仪表板。要了解某些值的概况,应该对它们进行总结。我的 SELECT 在我的 MySQL 数据库中运行良好,但如果我将它包含在我的 Node 项目中,那么我将不会得到任何输出。

app.get('/dash', function(req, res, next) {
connection.query("SELECT SUM(g_speicherplatz), SUM(g_spielzeit), SUM(g_kosten) FROM games", function(error, result, fields) {
if(error) {
req.flash('error', error)
res.render('games/g-dash', {
data: ''
})
} else {
res.render('games/g-dash', {
data: result,
})
}
})
})

EJS 输出:

<div class="container">
<div class="row">
<% if (data) {
console.log("Anzahl Datensätze: " + data.length);
data.forEach(function(dash){ %>
<div class="card mb-3">
<h3 class="card-header">Statistiken</h3>
<h3><%= dash.g_speicherplatz %></h3>
<h3><%= dash.g_spielzeit %></h3>
<h3><%= dash.g_kosten %></h3>
</div>
<% }) %>
<% } %>
</div>
</div>

我的错误在哪里?

最佳答案

您需要为总和分配别名。

    connection.query("SELECT SUM(g_speicherplatz) AS g_speicherplatz, SUM(g_spielzeit) AS g_spielzeit, SUM(g_kosten) AS g_kosten FROM games", function(error, result, fields) {

关于javascript - 带有 EJS 的 Node.js Express 项目中的 MySQL SUM 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56053996/

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