【问题标题】:MySQL query missing results in node-mysql that are returned in phpMyAdminMySQL 查询在 phpMyAdmin 中返回的 node-mysql 中缺少结果
【发布时间】: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


【解决方案1】:

对我来说,这是库中的一个错误,因为输出字段是有效且唯一的,但库没有正确处理文字点。

您应该得到s.resulthp.result,但是您会得到不带前缀的变量覆盖:result

MySQL 解决方案:使用别名 - SELECT s.result AS sresult, hp.result AS hpresult ...

node-mysql 解决方案:使用nestTables option for overlapping column names

从链接页面复制:

var options = {sql: '...', nestTables: true};
connection.query(options, function (error, results, fields) {
  if (error) throw error;
  /* results will be an array like this now:
  [{
    table1: {
      fieldA: '...',
      fieldB: '...',
    },
    table2: {
      fieldA: '...',
      fieldB: '...',
    },
  }, ...]
  */
});

或者作为替换文字点的字符串:

var options = {sql: '...', nestTables: '_'};
connection.query(options, function (error, results, fields) {
  if (error) throw error;
  /* results will be an array like this now:
  [{
    table1_fieldA: '...',
    table1_fieldB: '...',
    table2_fieldA: '...',
    table2_fieldB: '...',
  }, ...]
  */
});

【讨论】:

    【解决方案2】:

    问题可能是您的列在两个表中的名称相同(距离和结果)。 为了使 JSON 有效,您不能有重复的键,列的名称应该是唯一的。 尝试称它们为 distance1、result1、distance2、result2

    【讨论】:

      【解决方案3】:

      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}]
      

      【讨论】:

      • 感谢您的快速修复!我认为 Mladen 的建议是正确的,相同的列名可能是问题所在。
      猜你喜欢
      • 2020-06-20
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多