MySQL查询丢失导致在phpMyAdmin中返回的node
当我在phpMyAdmin中运行时,我的SQL查询返回四列,但在通过Node.js和mysql包执行时只返回两列。我怀疑它可能与异步性或LEFT JOIN有关,但我无法弄明白。
节点index.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}]
回答如下:
对node.js
零熟悉我会建议如下。尝试更改查询并向结果列添加名称。
例
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";
以上应该输出
[{sResult: "00:35:29", sDistance: 12, hpResult: "36:24", hpDistance: 12}]
MySQL查询丢失导致在phpMyAdmin中返回的node
当我在phpMyAdmin中运行时,我的SQL查询返回四列,但在通过Node.js和mysql包执行时只返回两列。我怀疑它可能与异步性或LEFT JOIN有关,但我无法弄明白。
节点index.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}]
回答如下:
对node.js
零熟悉我会建议如下。尝试更改查询并向结果列添加名称。
例
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";
以上应该输出
[{sResult: "00:35:29", sDistance: 12, hpResult: "36:24", hpDistance: 12}]