Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Node.js MySQL 删除


删除记录

您可以使用 "DELETE FROM" 语句从现有表中删除记录。

示例

删除地址为 "Mountain 21" 的任何记录。

var mysql = require('mysql');

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

con.connect(function(err) {
  if (err) throw err;
  var sql = "DELETE FROM customers WHERE address = 'Mountain 21'";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("已删除的记录数: " + result.affectedRows);
  });
});
运行示例 »

请注意 DELETE 语法中的 WHERE 子句:WHERE 子句指定应删除哪条或哪些记录。如果您省略 WHERE 子句,则所有记录都将被删除!

将以上代码保存到名为 "demo_db_delete.js" 的文件中并运行该文件。

运行 "demo_db_delete.js"

C:\Users\您的用户名>node demo_db_delete.js

这将为您提供以下结果

已删除的记录数: 1


结果对象

执行查询时,会返回一个结果对象。

结果对象包含有关查询如何影响表的信息。

以上示例返回的结果对象如下所示

{
  fieldCount: 0,
  affectedRows: 1,
  insertId: 0,
  serverStatus: 34,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}

这些属性的值可以这样显示

示例

返回受影响的行数

console.log(result.affectedRows)

这将产生以下结果

1

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.