Node.js MySQL 从中选择


从表中选择

要从 MySQL 中的表中选择数据,请使用 "SELECT" 语句。

示例

选择"customers"表中的所有记录,并显示结果对象:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query(" SELECT * FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});
运行示例 »

选择 *将返回全部

将上面的代码保存在名为 "demo_db_select.js" 的文件中并运行该文件:

运行"demo_db_select.js"

C:\Users\ Your Name>node demo_db_select.js

这会给你这个结果:

[
  { id: 1, name: 'John', address: 'Highway 71'},
  { id: 2, name: 'Peter', address: 'Lowstreet 4'},
  { id: 3, name: 'Amy', address: 'Apple st 652'},
  { id: 4, name: 'Hannah', address: 'Mountain 21'},
  { id: 5, name: 'Michael', address: 'Valley 345'},
  { id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
  { id: 7, name: 'Betty', address: 'Green Grass 1'},
  { id: 8, name: 'Richard', address: 'Sky st 331'},
  { id: 9, name: 'Susan', address: 'One way 98'},
  { id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
  { id: 11, name: 'Ben', address: 'Park Lane 38'},
  { id: 12, name: 'William', address: 'Central st 954'},
  { id: 13, name: 'Chuck', address: 'Main Road 989'},
  { id: 14, name: 'Viola', address: 'Sideway 1633'}
]


选择列

要仅选择表中的部分列,请使用 "SELECT" 语句,后跟列名称。

示例

从 "customers" 表中选择名称和地址,并显示返回对象:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query(" SELECT name, address FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});
运行示例 »

将上面的代码保存在名为 "demo_db_select2.js" 的文件中并运行该文件:

运行"demo_db_select2.js"

C:\Users\ Your Name>node demo_db_select2.js

这会给你这个结果:

[
  { name: 'John', address: 'Highway 71'},
  { name: 'Peter', address: 'Lowstreet 4'},
  { name: 'Amy', address: 'Apple st 652'},
  { name: 'Hannah', address: 'Mountain 21'},
  { name: 'Michael', address: 'Valley 345'},
  { name: 'Sandy', address: 'Ocean blvd 2'},
  { name: 'Betty', address: 'Green Grass 1'},
  { name: 'Richard', address: 'Sky st 331'},
  { name: 'Susan', address: 'One way 98'},
  { name: 'Vicky', address: 'Yellow Garden 2'},
  { name: 'Ben', address: 'Park Lane 38'},
  { name: 'William', address: 'Central st 954'},
  { name: 'Chuck', address: 'Main Road 989'},
  { name: 'Viola', address: 'Sideway 1633'}
]

结果对象

从上面示例的结果可以看出,结果对象是一个数组,其中包含每一行作为一个对象。

要返回第三条记录的地址,只需引用第三个数组对象的地址属性:

示例

返回第三条记录的地址:

console.log(result[2].address);

这将产生这样的结果:

Apple st 652

字段对象

回调函数的第三个参数是一个数组,其中包含结果中每个字段的信息。

示例

选择 "customers" 表中的所有记录,并显示领域目的:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT name, address FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log( fields);
  });
});
运行示例 »

将上面的代码保存在名为 "demo_db_select_fields.js" 的文件中并运行该文件:

运行"demo_db_select_fields.js"

C:\Users\ Your Name>node demo_db_select_fields.js

这会给你这个结果:

[
  {
    catalog: 'def',
    db: 'mydb',
    table: 'customers',
    orgTable: 'customers',
    name: 'name',
    orgName: 'name',
    charsetNr: 33,
    length: 765,
    type: 253,
    flags: 0,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true
  },
  {
    catalog: 'def',
    db: 'mydb',
    table: 'customers',
    orgTable: 'customers',
    name: 'address',
    orgName: 'address',
    charsetNr: 33,
    length: 765,
    type: 253,
    flags: 0,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true
  }
]

从上面示例的结果中可以看出,fields 对象是一个数组,其中包含每个字段作为对象的信息。

要返回第二个字段的名称,只需引用第二个数组项的 name 属性:

示例

返回第二个字段的名称:

console.log(fields[1].name);

这将产生这样的结果:

address