Node.js-从MySQL获取值并将其分配给变量

时间:2020-05-31 07:54:03

标签: mysql node.js

我的查询:

pool.query("SELECT MAX(ID) FROM `games` WHERE status IN('0','1') LIMIT 1", (err, row) => {
    if(err) return console.log("err getting the game.");
    currentGame = row[0];
    console.log(currentGame);
});

当前结果:

RowDataPacket { 'MAX(ID)': 1 }

所需结果:

1

如何仅获取值而不包含其他内容?

1 个答案:

答案 0 :(得分:1)

尝试为您的计数查询添加别名,然后访问它:

pool.query("SELECT MAX(ID) AS max_id FROM games WHERE status IN ('0','1')", (err, row) => {
    if(err) return console.log("err getting the game.");
    currentGame = row[0].max_id;
    console.log(currentGame);
});

注意:根据定义,最大查询将始终仅返回单个记录结果集(在没有GROUP BY的情况下),因此不需要LIMIT 1