【发布时间】:2018-06-04 05:09:21
【问题描述】:
当我在 phpMyAdmin 中运行时,我的 SQL 查询返回四列,但通过 Node.js 和 mysql 包执行时只有两列。我怀疑这可能与异步性或 LEFT JOIN 有关,但我无法弄清楚。
节点索引.js:
app.get('/name', function (req,res){
var query='SELECT s.result, s.distance, hp.result, hp.distance FROM `table15` s LEFT JOIN `table14` hp on s.name = hp.name WHERE s.name = "John Appleseed"';
pool.query(query,function(err, result) {
if (err) throw err;
console.log(result);
res.json(result);
});
});
app.js
$.ajax({
url : "/name",
type : "GET",
success : function(data){
var len = data.length;
console.log(data);
}
});
在浏览器和控制台中返回:
[{result: "36:24", distance: 12}]
但在 phpMyAdmin 中,我得到了四列,因此在浏览器中也会出现这样的情况:
[{result: "00:35:29", distance: 12, result: "36:24", distance: 12}]
【问题讨论】:
-
您是否尝试过更改您的查询,例如:
SELECT s.result AS sResult, s.distance AS sDistance, hp.result AS hpResult, hp.distance AS hpDistance FROM table15 s LEFT JOIN table14 hp on s.name = hp.name WHERE s.name = "John Appleseed"? -
你用的是哪个mysql-node版本?
-
服务器端
console.log的输出是什么?和ajax输出一样吗? -
在 GitHub 提交问题:github.com/mysqljs/mysql/issues/1904
标签: mysql node.js ajax phpmyadmin