NodeJS 基礎(chǔ)教程

NodeJS Express.js

NodeJS 緩沖&URL

NodeJS MySql

NodeJS MongoDB

NodeJS 文件(FS)

NodeJS 其他

Node.js MySQL DELETE

Node.js MySQL刪除記錄

Node.js MySQL刪除查詢可幫助您基于過濾條件從表中刪除一個(gè)或多個(gè)記錄。

  • MySQL根據(jù)過濾器刪除一些記錄

  • MySQL從表中刪除所有記錄

Node.js MySQL –根據(jù)條件從表中刪除記錄

在表上的記錄的一個(gè)或多個(gè)屬性上應(yīng)用過濾器的情況下,對指定的表執(zhí)行DELETE FROM查詢。

刪除記錄之前學(xué)生表的內(nèi)容

mysql> select * from students; 
+---------------+--------+-------+
| name | rollno | marks |
+---------------+--------+-------+
| John | 1 | 74 |
| Arjun | 2 | 74 |
| Prasanth | 3 | 77 |
| Adarsh | 4 | 78 |
| Raja | 5 | 94 |
| Sai | 6 | 84 |
| Ross | 7 | 54 |
| Monica Gellar | 8 | 86 |
| Lee | 9 | 98 |
| Bruce Wane | 10 | 92 |
| Sukumar | 11 | 99 |
| Anisha | 12 | 95 |
| Miley | 13 | 85 |
| Jobin | 14 | 87 |
| Jack | 16 | 82 |
| Priya | 17 | 88 |
+---------------+--------+-------+
16 rows in set (0.00 sec)
 // 引入mysql模塊 
 var mysql = require('mysql'); 
 
 // 創(chuàng)建具有所需詳細(xì)信息的連接變量 
 var con = mysql.createConnection({ 
  host: "localhost", // 運(yùn)行mysql的服務(wù)器的IP地址 
  user: "arjun", // mysql數(shù)據(jù)庫的用戶名
  password: "password", // 對應(yīng)的密碼
  database: "studentsDB" // 使用指定的數(shù)據(jù)庫
 }); 
 
 // 連接到數(shù)據(jù)庫。 
 con.connect(function(err) { 
  if (err) throw err; 
  // 如果連接成功 
  con.query("DELETE FROM students WHERE rollno>10", function (err, result, fields) { 
    // 如果在執(zhí)行上述查詢時(shí)出現(xiàn)任何錯(cuò)誤,則拋出錯(cuò)誤 
    if (err) throw err; 
    // 如果沒有錯(cuò)誤,您將得到結(jié)果 
    console.log(result); 
  }); 
 });

運(yùn)行deleteRecordsFiltered.js-終端輸出

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node deleteRecordsFiltered.js 
OkPacket { 
  fieldCount: 0, 
  affectedRows: 6, 
  insertId: 0, 
  serverStatus: 34, 
  warningCount: 0, 
  message: '', 
  protocol41: true, 
  changedRows: 0 }

觀察結(jié)果,受影響的行為6,這意味著已刪除6條記錄。

以下是在學(xué)生表上執(zhí)行MySQL DELETE FROM查詢之后剩下的記錄。

刪除后的學(xué)生表格內(nèi)容

mysql> select * from students; 
+---------------+--------+-------+
| name | rollno | marks |
+---------------+--------+-------+
| John | 1 | 74 |
| Arjun | 2 | 74 |
| Prasanth | 3 | 77 |
| Adarsh | 4 | 78 |
| Raja | 5 | 94 |
| Sai | 6 | 84 |
| Ross | 7 | 54 |
| Monica Gellar | 8 | 86 |
| Lee | 9 | 98 |
| Bruce Wane | 10 | 92 |
+---------------+--------+-------+
10 rows in set (0.00 sec)

Node.js MySQL –從表中刪除所有記錄

// 引入mysql模塊
var mysql = require('mysql'); 
 
// 創(chuàng)建具有所需詳細(xì)信息的連接變量
var con = mysql.createConnection({ 
  host: "localhost", // 運(yùn)行mysql的服務(wù)器的IP地址
  user: "arjun", // mysql數(shù)據(jù)庫的用戶名
  password: "password", // 對應(yīng)的密碼
  database: "studentsDB" // 使用指定的數(shù)據(jù)庫
 }); 
 
// 連接到數(shù)據(jù)庫。
con.connect(function(err) { 
  if (err) throw err; 
  // 如果連接成功
  con.query("DELETE FROM students", function (err, result, fields) { 
    // 如果在執(zhí)行上述查詢時(shí)出現(xiàn)任何錯(cuò)誤,則拋出錯(cuò)誤
    if (err) throw err; 
    // 如果沒有錯(cuò)誤,您將得到結(jié)果
    console.log(result); 
  }); 
 });

運(yùn)行deleteRecordsAll.js-終端輸出

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node deleteRecordsAll.js 
OkPacket { 
  fieldCount: 0, 
  affectedRows: 10, 
  insertId: 0, 
  serverStatus: 34, 
  warningCount: 0, 
  message: '', 
  protocol41: true, 
  changedRows: 0 }

以下是執(zhí)行后的students表的內(nèi)容

MySQL Table students 內(nèi)容

mysql> select * from students; 
Empty set (0.00 sec)

結(jié)論:

在此Node.js教程– Node.js MySQL – DELETE FROM中,我們學(xué)習(xí)了根據(jù)條件刪除記錄或從表中刪除所有記錄。

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清