Hướng dẫn nodejs mysql update multiple columns - nodejs mysql cập nhật nhiều cột

4

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Cách cập nhật nhiều cột trong MySQL bằng Node.js:

var query = 'UPDATE employee SET profile_name = ? WHERE id = ?';
connection.query(query,[req.name,req.id] function (error, result, rows, fields) {

Nhưng tôi phải cập nhật ____10,

var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
1,
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
2,
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
3,
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
4 cùng một lúc. Làm thế nào tôi có thể làm điều đó, bất cứ ai có thể đề xuất.
How can I do that, can anyone suggest.

Hướng dẫn nodejs mysql update multiple columns - nodejs mysql cập nhật nhiều cột

Arulkumar

12.7K14 Huy hiệu vàng49 Huy hiệu bạc66 Huy hiệu đồng14 gold badges49 silver badges66 bronze badges

Hỏi ngày 14 tháng 9 năm 2016 lúc 6:49Sep 14, 2016 at 6:49

3

Chỉ cần thêm tất cả các cột trong tập:

var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {

Đã trả lời ngày 14 tháng 9 năm 2016 lúc 6:51Sep 14, 2016 at 6:51

JensjensJens

65K15 Huy hiệu vàng92 Huy hiệu bạc106 Huy hiệu Đồng15 gold badges92 silver badges106 bronze badges

3

Để cập nhật nhiều cột của bạn trong MySQL bằng NodeJS, thì bạn có thể làm điều đó như mã này bên dưới: 👇To update your multiple columns in mysql using nodejs, then You can do it like this code below: 👇

const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});

Đảm bảo rằng

var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
5 của bạn không phải là
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
6 và trường trong
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
5 của bạn, nó giống nhau với trường trong bảng
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
8 của bạn.Make sure your
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
5 is not
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
6 and the field in your
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
5 it's same with the field in your
var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
8 table.

Nếu

var query = 'UPDATE employee SET profile_name = ?, phone =?, .. WHERE id=?';

connection.query(query,[req.name,req.phone,...,req.id] function (error, result, rows, fields) {
5 của bạn là
const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
0 hoặc
const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
1, thì bạn có thể thêm phần mềm trung gian này vào máy chủ Express của mình:

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

Tôi hy vọng nó có thể giúp bạn.

Đã trả lời ngày 29 tháng 1 năm 2020 lúc 14:07Jan 29, 2020 at 14:07

Hướng dẫn nodejs mysql update multiple columns - nodejs mysql cập nhật nhiều cột

const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
2 Cú pháp tuyên bố:

UPDATE <TableName>
SET <Col1> = <Val1>,
    <Col2> = <Val2>,
    ....
WHERE id = ?

Đã trả lời ngày 14 tháng 9 năm 2016 lúc 6:52Sep 14, 2016 at 6:52

Sagisagisagi

39K5 Huy hiệu vàng56 Huy hiệu bạc82 Huy hiệu Đồng5 gold badges56 silver badges82 bronze badges

Nếu bạn có nhiều cột cập nhật và cần lấy các giá trị từ đối tượng, bạn có thể thực hiện những điều sau-

let data = {
    "table": {
        "update_table":"dlrecustomer"
    },
  "result": {
    "pro":"blre",
        "pro_id":"BFCA",
        "MOBILE":"9506443333",
  },
  "keys": {
    "CUSTOMER":"27799144",
        "APPLICATION":"5454642463"
  },
}

let update_set = Object.keys(data.result).map(value=>{
       return ` ${value}  = "${data.result[value]}"`;
    });

    let update_query =  `UPDATE ${data.table.update_table} SET ${update_set.join(" ,")} WHERE CUST_ID = "${data.keys.CUSTOMER}" AND APPL_ID = "${data.keys.APPLICATION}"`;

Đã trả lời ngày 6 tháng 2 năm 2019 lúc 12:17Feb 6, 2019 at 12:17

Hướng dẫn nodejs mysql update multiple columns - nodejs mysql cập nhật nhiều cột

Patelnayan PatelNayan Patel

1.62523 huy hiệu bạc26 Huy hiệu đồng23 silver badges26 bronze badges

1

Đăng vào ngày 28 tháng 10 năm 2021

Tìm hiểu cách cập nhật nhiều cột trong máy chủ cơ sở dữ liệu MySQL bằng cách sử dụng một câu lệnh cập nhật duy nhất


Câu lệnh MySQL

const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
2 có thể được sử dụng để cập nhật nhiều cột cùng một lúc.

Để làm như vậy, bạn cần chỉ định các cột và các giá trị mới mà bạn muốn cập nhật bên cạnh mệnh đề

const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
4.

Cú pháp sẽ trông như sau:

UPDATE table_name 
  SET column_1 = value_1,
      column_2 = value_2,
      column_3 = value_3,
      ...
  WHERE ...

Ví dụ: giả sử bạn có một bảng gọi là

const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
5 với dữ liệu sau:

+----+--------+---------+------+
| id | owner  | species | age  |
+----+--------+---------+------+
|  1 | Jessie | bird    |    2 |
|  2 | Ann    | duck    |    3 |
|  3 | Joe    | horse   |    4 |
|  4 | Mark   | dog     |    4 |
|  5 | Peter  | dog     |    5 |
+----+--------+---------+------+

Ở đây, một câu lệnh

const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
2 để cập nhật nhiều cột:

UPDATE pets
  SET `owner` = "Ronald",
      `species` = "cat",
      `age` = 1
  WHERE id = 5;

Sau khi được thực thi, thì dữ liệu trên hàng với

const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
7 sẽ được cập nhật như hiển thị bên dưới:

+----+--------+---------+------+
| id | owner  | species | age  |
+----+--------+---------+------+
|  1 | Jessie | bird    |    2 |
|  2 | Ann    | duck    |    3 |
|  3 | Joe    | horse   |    4 |
|  4 | Mark   | dog     |    4 |
|  5 | Ronald | cat     |    1 |
+----+--------+---------+------+

Và đó là cách mà bạn có thể cập nhật nhiều cột trong MySQL.

Don Tiết quên chỉ định mệnh đề

const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
8 hoặc bạn có thể gặp mã lỗi 1175 khi chạy câu lệnh
const query = 'UPDATE `employee` SET ? WHERE ?';
connection.query(query, [req.body, req.params], function(err, rows) {
  if(err) {
    console.log(err.message);
    // do some stuff here
  } else {
    console.log(rows);
    // do some stuff here
  }
});
2.

Làm cách nào để cập nhật nhiều cột trong MySQL?

Lệnh cập nhật MySQL có thể được sử dụng để cập nhật nhiều cột bằng cách chỉ định danh sách phân tách dấu phẩy của cột_name = new_value. Trong đó cột_name là tên của cột sẽ được cập nhật và new_value là giá trị mới mà cột sẽ được cập nhật.by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.

Làm thế nào để bạn cập nhật nhiều cột trong SQL với các điều kiện khác nhau?

Để cập nhật nhiều cột, sử dụng mệnh đề đặt để chỉ định các cột bổ sung.Giống như với các cột đơn, bạn chỉ định một cột và giá trị mới của nó, sau đó một tập hợp cột và giá trị khác.Trong trường hợp này, mỗi cột được phân tách bằng một cột.use the SET clause to specify additional columns. Just like with the single columns you specify a column and its new value, then another set of column and values. In this case each column is separated with a column.

Làm cách nào để cập nhật tất cả các giá trị trong một cột trong MySQL?

Để đặt tất cả các giá trị trong một truy vấn MySQL duy nhất, bạn có thể sử dụng lệnh Update.Cú pháp như sau.Cập nhật YourTableName Đặt của bạnColumnName = yourValue;Để hiểu cú pháp trên, chúng ta hãy tạo bảng.update yourTableName set yourColumnName =yourValue; To understand the above syntax, let us create a table.

Làm thế nào để bạn cập nhật nhiều cột của nhiều hàng trong một câu lệnh SQL?

Đầu tiên, chỉ định tên bảng mà bạn muốn thay đổi dữ liệu trong mệnh đề cập nhật.Thứ hai, gán một giá trị mới cho cột mà bạn muốn cập nhật.Trong trường hợp bạn muốn cập nhật dữ liệu trong nhiều cột, mỗi cặp cột = giá trị được phân tách bằng dấu phẩy (,).Thứ ba, chỉ định các hàng bạn muốn cập nhật trong mệnh đề WHERE.each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.