MySQL 中是否存在 NOT EQUAL?

mysqlmysqli database

是的,MySQL 中 <> 运算符的形式存在 NOT EQUAL。语法如下 −

SELECT * FROM yourTableName WHERE yourColumnName <> yourValue;

为了理解上述语法,让我们创建一个表。创建表的查询如下−

mysql> create table DoesNotEqualDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.98 sec)

使用 insert 命令在表中插入一些记录。插入记录的查询如下−

mysql> insert into DoesNotEqualDemo(Name) values(NULL);
Query OK, 1 row affected (0.24 sec)

mysql> insert into DoesNotEqualDemo(Name) values('John');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DoesNotEqualDemo(Name) values('Carol');
Query OK, 1 row affected (0.43 sec)

mysql> insert into DoesNotEqualDemo(Name) values('Bob');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DoesNotEqualDemo(Name) values('');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DoesNotEqualDemo(Name) values('Larry');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DoesNotEqualDemo(Name) values(NULL);
Query OK, 1 row affected (0.10 sec)

使用 select 语句显示表中的所有记录。查询如下−

mysql> select *from DoesNotEqualDemo;

以下是输出 −

+----+-------+
| Id | Name  |
+----+-------+
|  1 | NULL  |
|  2 | John  |
|  3 | Carol |
|  4 | Bob   |
|  5 |       |
|  6 | Larry |
|  7 | NULL  |
+----+-------+
7 rows in set (0.00 sec)

以下查询用于选择所有不等于 NULL 以及空字符串的记录 −

mysql> select *from DoesNotEqualDemo where Name <> 'NULL' and Name <> '';

以下是输出 −

+----+-------+
| Id | Name  |
+----+-------+
|  2 | John  |
|  3 | Carol |
|  4 | Bob   |
|  6 | Larry |
+----+-------+
4 rows in set (0.00 sec)

相关文章