
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送電子郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL WHERE 條件
- Node.js - MySQL 排序
- Node.js - MySQL 刪除
- Node.js - MySQL 更新
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除
- Node.js - MongoDB 更新
- Node.js - MongoDB 限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - MySQL 刪除
在對 MySQL 資料庫執行的 CRUD 操作中,DELETE 查詢用於從表中刪除一行或多行。在本章中,我們將演示如何在 Node.js 應用程式中呼叫 MySQL DELETE 語句。
基本的 DELETE 查詢具有以下語法:
DELETE FROM table_name WHERE condition;
儘管 WHERE 子句是可選的,但大多數情況下都會使用它,否則它將刪除表中的所有行。
簡單的 DELETE
在下面的 Node.js 程式碼中,DELETE 查詢字串傳遞給 mysql.query() 方法。程式將刪除所有年齡大於 25 的員工記錄。
示例
var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "root", password: "mypassword", database: "mydb" }); var qry ="DELETE FROM employee WHERE age>25;"; con.connect(function (err) { if (err) throw err; console.log("Connected!"); con.query(qry, function (err, results) { if (err) throw err; console.log(results); }); con.end(); });
輸出
OkPacket { fieldCount: 0, affectedRows: 0, insertId: 0, serverStatus: 34, warningCount: 0, message: '', protocol41: true, changedRows: 0 }
從上面程式碼中的查詢字串中刪除 WHERE 子句。
var qry ="DELETE FROM employee;"; con.connect(function (err) { if (err) throw err; console.log("Connected!"); con.query(qry, function (err, results) { if (err) throw err; console.log(results); }); con.end(); });
現在開啟命令列客戶端並執行 SELECT 查詢
mysql> select * from employee; Empty set (0.00 sec)
您可以看到表中沒有任何記錄剩餘。不帶 WHERE 子句的 DELETE 查詢等效於 TRUNCATE 語句。
var qry ="TRUNCATE employee;"; con.connect(function (err) { if (err) throw err; console.log("Connected!"); con.query(qry, function (err, results) { if (err) throw err; console.log(results); }); con.end(); });
LIMIT 子句
DELETE 查詢中的 LIMIT 子句將刪除操作限制為指定數量。例如,LIMIT 5 只刪除給定順序中的前 5 條記錄。對於此示例,我們將使用預安裝的 world 資料庫及其中的 city 表。
示例
var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "root", password: "mypassword", database: "world" }); var qry ="DELETE FROM city WHERE CountryCode='IND' ORDER BY population LIMIT 5;"; con.connect(function (err) { if (err) throw err; console.log("Connected!"); con.query(qry, function (err, results) { if (err) throw err; console.log(results); }); con.end(); });
輸出
OkPacket { fieldCount: 0, affectedRows: 5, insertId: 0, serverStatus: 2, warningCount: 0, message: '', protocol41: true, changedRows: 0 }
最初,City 表有 341 行,CountryCode 為 IND。透過執行上述程式碼刪除人口按升序排列的前 5 個城市後,表中將剩下 336 個城市。您可以透過在執行上述程式碼之前和之後執行 SELECT COUNT 查詢來檢查它。
mysql> SELECT COUNT(name) from city WHERE CountryCode='IND';
廣告