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?

Viewed 29

There are three types of customers in my CSV data and I only want to display Type1... I have written two separate code but I want to combine them into one:

This one is to filter specific columns:

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);
  });
});

This one is to filter by 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();
1 Answers

This would show the count type for every row in customers

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
Related