【问题标题】:JSON result is showing command of SUM MySQLJSON 结果显示 SUM MySQL 的命令
【发布时间】:2018-02-28 22:15:38
【问题描述】:

我正在对我的 MYSQL 表的所有值列进行有效的返回计算,但是当我去检查我的 api 结果时;列值求和命令出现在 JSON 的结果中。

   [
      {
        "SUM(producFinalPrice)": 6000
      }
    ]

这是我的路由器从我的数据库中获取数据。

router.get('/sell/home-card-last-sell-compared-to-the-previous-day', function (req, res) {
  connection.query('SELECT SUM(producFinalPrice) FROM new_sell',
    function (err, lastSellComparedToThePreviousDayCardHome, fields) {
      if (err) {
        throw err;
      }
      res.send(lastSellComparedToThePreviousDayCardHome);
    }
  );
});

`

为了更清楚地说明我的错误,我正在 AJAX 中的上一个查询中发出最终请求;通过这种方式。

$(function () {
  $.ajax({
    type: "GET",
    url: "/sell/home-card-last-sell-compared-to-the-previous-day",
    success: function (lastSellComparedToThePreviousDayCardHome) {
      var $lastSellComparedPreviousDay = $('#last-sell-compared-to-the-previous-day');
      $.each(lastSellComparedToThePreviousDayCardHome, function (i, SellPreviousDay) {
        $lastSellComparedPreviousDay.append('<li>R$ ' + SellPreviousDay.producFinalPrice + '.</li>');
      });
      console.log('Sucess return to Last Sell Previous Day!', lastSellComparedToThePreviousDayCardHome);
    }
  });
});

基本上就是这样,我找不到任何可以帮助我的东西.. 感谢您帮助我;]

【问题讨论】:

    标签: mysql json node.js ajax express


    【解决方案1】:

    使用别名:

    connection.query('SELECT SUM(producFinalPrice) AS productFinalSum FROM new_sell', ...
                                                   ^^^^^^^^
    

    然后,当您在 Node 中解析 JSON 时,检查 productFinalSum 键。实际上,我认为您甚至可以使用 SUM(productFinalSum) 来访问您的 JSON,但它看起来很尴尬。

    【讨论】:

    • NICE 完美运行。谢谢蒂姆 b。我在 8 分钟内标记为已回答 xD
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多