📜  Node.js MySQL NULL 值

📅  最后修改于: 2022-05-13 01:56:23.073000             🧑  作者: Mango

Node.js MySQL NULL 值

在本文中,将学习处理NULL值并在NULL值的基础上进行 Query。

句法:

IS NULL;
IS NOT NULL;

返回值:

  • 'IS NULL' 返回包含一个或多个 NULL 值的列中的行。
  • 'IS NOT NULL' 返回列中不包含任何 NULL 值的行。

模块安装:使用以下命令安装 MySQL 模块。

npm install mysql

数据库:我们的带有示例数据的 SQL 发布者表预览如下所示。

示例 1:

index.js
const mysql = require("mysql");
  
let db_con  = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: '',
    database: 'gfg_db'
});
  
db_con.connect((err) => {
    if (err) {
      console.log("Database Connection Failed !!!", err);
      return;
    }
  
    console.log("We are connected to gfg_db database");
  
    // Here is the query
    let query = "SELECT * FROM users WHERE address IS NULL";
  
    db_con.query(query, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});


index.js
const mysql = require("mysql");
  
let db_con  = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: '',
    database: 'gfg_db'
});
  
db_con.connect((err) => {
    if (err) {
      console.log("Database Connection Failed !!!", err);
      return;
    }
  
    console.log("We are connected to gfg_db database");
  
    // Here is the query
    let query = "SELECT * FROM users WHERE address IS NOT NULL";
  
    db_con.query(query, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});


输出:

示例 2:

index.js

const mysql = require("mysql");
  
let db_con  = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: '',
    database: 'gfg_db'
});
  
db_con.connect((err) => {
    if (err) {
      console.log("Database Connection Failed !!!", err);
      return;
    }
  
    console.log("We are connected to gfg_db database");
  
    // Here is the query
    let query = "SELECT * FROM users WHERE address IS NOT NULL";
  
    db_con.query(query, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});

输出: