Posts Tagged ‘sql’

Working together – Node.js and mysql

July 17, 2012

This article assumes that Node.js and mysql are installed.

Type in, the command below, to install the mysql module.

npm install mysql@2.0.0-alpha3

Listed below, is a sample program, mysql.js, for connecting to mysql.

var mysql      = require('mysql');

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'user',
  password : 'secret',
  database : 'database name'
});

connection.connect();

connection.query('SELECT * from contact', function(err, rows, fields) {
  if (err) throw err;

  console.log("Number of rows: " + rows.length);
  console.log('Query result: ', rows);

  var columns=new Array(fields.length);
  for (var j = 0; j < fields.length; ++j) {
	columns[j] = fields[j]['name'];
  }

  for (var i in rows) {
	var row = rows[i];
	//console.log(row.FirstName);
	for (var j = 0; j < fields.length; ++j) {
		console.log(columns[j], ': ', row[columns[j]]);
	}	
  }

});

//Escaping query values
var contactId = 1;
var sql    = 'SELECT * FROM Contact WHERE ContactID = ' + connection.escape(contactId);
connection.query(sql, function(err, rows, fields) {
  if (err) throw err;

  console.log("Number of rows: " + rows.length);
  console.log('Query result: ', rows);
});

//Escaping query values  
var contactId = 2;
var sql    = 'SELECT * FROM Contact WHERE Contactid = ?';
connection.query(sql, [contactId], function(err, rows, fields) {
  if (err) throw err;

  console.log("Number of rows: " + rows.length);
  console.log('Query result: ', rows);
});

//Insert
var post  = {Dated: new Date(), FirstName: 'Jane', LastName: 'Smith'};
var query = connection.query('INSERT INTO Contact SET ?', post, function(err, rows, fields) {
  if (err) throw err;
  console.log(query.sql); 
  console.log(rows.insertId);
});

connection.end();

References

felixge / node-mysql

Two tables, identical schema, two columns only, table A and table B. For each product, select the maximum price.

September 2, 2009

/*
Two tables, identical schema, two columns only, table A and table B.
First column is product number, second column is price.
Table A Product 1, $20. Product 2, $20.
Table B Product 2, $30. Product 3, $20.
For each product, select the maximum price.
*/

CREATE TABLE TableA
(
ProductNumber     VARCHAR(20) NOT NULL,
Price        DECIMAL
)
GO

CREATE TABLE TableB
(
ProductNumber     VARCHAR(20) NOT NULL,
Price        DECIMAL
)
GO

INSERT INTO TableA
(
ProductNumber,
Price
)
VALUES
(
‘Product 1’,
20
)
GO

INSERT INTO TableA
(
ProductNumber,
Price
)
VALUES
(
‘Product 2’,
20
)
GO

INSERT INTO TableB
(
ProductNumber,
Price
)
VALUES
(
‘Product 2’,
30
)
GO

INSERT INTO TableB
(
ProductNumber,
Price
)
VALUES
(
‘Product 3’,
20
)
GO

CREATE PROCEDURE TwoIdenticalTablesMaxPrice
AS
–For each product, select the maximum price.

–Declare work table variable.
DECLARE @MultipleTablesGrouping AS Table(ProductNumber VARCHAR(20), Price DECIMAL)

–Populate work table variable.
INSERT INTO @MultipleTablesGrouping
SELECT
ProductNumber,
Max(Price)
FROM
TableA
GROUP BY
ProductNumber
UNION
SELECT
ProductNumber,
Max(Price)
FROM
TableB
GROUP BY
ProductNumber

–Declare work table variable.
DECLARE @SingleTableMaximumPrice AS Table(ProductNumber VARCHAR(20), Price DECIMAL)

INSERT INTO @SingleTableMaximumPrice
SELECT
ProductNumber,
Max(Price)
FROM
@MultipleTablesGrouping
GROUP BY
ProductNumber

–Display result
SELECT * FROM @SingleTableMaximumPrice
GO

EXEC TwoIdenticalTablesMaxPrice
GO

DROP PROCEDURE TwoIdenticalTablesMaxPrice
GO

DROP TABLE TableA
GO

DROP TABLE TableB
GO