Node.js MySQL Select From 语句

从表格中选择数据

要从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);
  });
});
运行实例 »

SELECT * 将返回所有列

将上述代码保存在名为 "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'}
]

Result 记录集对象

从上面示例的结果中可以看到,result 对象是一个数组,包含作为记录集对象的每一行。

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

实例

返回第 3 条记录的地址:

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

返回结果:

Apple st 652

Fields 字段对象

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

实例

从 "customers" 表中选择所有记录,并显示 fields 对象:

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: 'address',
    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 对象是一个数组,包含每个字段作为对象的信息。

要返回第 2 个字段的名称,只需参考第 2 个数组项的 name 属性:

实例

返回第 2 个字段的名称:

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

返回结果:

address