Node.js MySQL Chọn Từ


Chọn từ một bảng

Để chọn dữ liệu từ một bảng trong MySQL, hãy sử dụng câu lệnh "SELECT".

Thí dụ

Chọn tất cả các bản ghi từ bảng "khách hàng" và hiển thị đối tượng kết quả:

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 * sẽ trả về tất cả các cột

Lưu mã ở trên vào tệp có tên "demo_db_select.js" và chạy tệp:

Chạy "demo_db_select.js"

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

Điều này sẽ cung cấp cho bạn kết quả này:

[
  { 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'}
]


Chọn cột

Để chỉ chọn một số cột trong bảng, hãy sử dụng câu lệnh "CHỌN" theo sau là tên cột.

Thí dụ

Chọn tên và địa chỉ từ bảng "khách hàng" và hiển thị đối tượng trả lại:

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

Lưu đoạn mã trên vào một tệp có tên "demo_db_select2.js" và chạy tệp:

Chạy "demo_db_select2.js"

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

Điều này sẽ cung cấp cho bạn kết quả này:

[
  { 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'}
]

Đối tượng kết quả

Như bạn có thể thấy từ kết quả của ví dụ trên, đối tượng kết quả là một mảng chứa mỗi hàng là một đối tượng.

Để trả về ví dụ: địa chỉ của bản ghi thứ ba, chỉ cần tham chiếu đến thuộc tính địa chỉ của đối tượng mảng thứ ba:

Thí dụ

Trả lại địa chỉ của bản ghi thứ ba:

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

Điều nào sẽ tạo ra kết quả này:

Apple st 652

Đối tượng trường

Tham số thứ ba của hàm gọi lại là một mảng chứa thông tin về mỗi trường trong kết quả.

Thí dụ

Chọn tất cả các bản ghi từ bảng "khách hàng" và hiển thị đối tượng trường :

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

Lưu đoạn mã trên vào tệp có tên "demo_db_select_fields.js" và chạy tệp:

Chạy "demo_db_select_fields.js"

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

Điều này sẽ cung cấp cho bạn kết quả này:

[
  {
    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
  }
]

Như bạn có thể thấy từ kết quả của ví dụ trên, đối tượng trường là một mảng chứa thông tin về mỗi trường như một đối tượng.

Để trả về ví dụ: tên của trường thứ hai, chỉ cần tham chiếu đến thuộc tính tên của mục mảng thứ hai:

Thí dụ

Trả lại tên của trường thứ hai:

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

Điều nào sẽ tạo ra kết quả này:

address