【问题标题】:I want to combine both nodejs programs in order to get one result: specific CSV data filtered by type - how do I combine the code properly?我想结合两个 nodejs 程序以获得一个结果:按类型过滤的特定 CSV 数据 - 如何正确结合代码?
【发布时间】:2021-05-17 23:39:22
【问题描述】:

我的 CSV 数据中有三种类型的客户,我只想显示 Type1... 我已经编写了两个单独的代码,但我想将它们合并为一个:

这个是过滤特定列的:

var mysql = require('mysql');
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "private",
    database: "private"
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT customernumber, type, gender, name, streetapt, city, state, zip, phone, discount, outstandingbalance FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});

这个是按TYPE过滤的:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'private',
  database : 'private'
});
connection.connect();

connection.query('SELECT type, COUNT(type) FROM icedcoffee.customers group by type', function(err, rows, fields) 
{
  if (err) throw err;
  console.log(rows[1]);
  console.log(rows[0]);
  console.log(rows[2]);
});

connection.end();

【问题讨论】:

    标签: mysql node.js database csv


    【解决方案1】:

    这将显示客户中每一行的计数类型

    SELECT customernumber, c.type, gender, name, streetapt, city, state, zip, phone, discount, outstandingbalance, t1.ctype 
    FROM customers c INNER JOIN (SELECT type, COUNT(type) ctype FROM icedcoffee.customers group by type) t1  ON c.type = t1.type
    WHERE c.type = 1
    

    【讨论】:

    • 谢谢。但是有三种类型的客户,我只尝试显示类型 1。
    • 只需添加一个 where 子句。您还可以过滤子查询
    • connection.query('SELECT * FROM icedcoffee.customers WHERE type = 1', function(err, rows, feilds) { if (err) throw err; console.log(rows); });
    • ^ 那是我的最终答案。谢谢!
    • 我还以为你想要结合 bith,但不要忘记
    猜你喜欢
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2021-04-11
    相关资源
    最近更新 更多