是的,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) -> );
使用insert命令在表中插入一些记录。插入记录的查询如下-
mysql> insert into DoesNotEqualDemo(Name) values(NULL); mysql> insert into DoesNotEqualDemo(Name) values('John'); mysql> insert into DoesNotEqualDemo(Name) values('Carol'); mysql> insert into DoesNotEqualDemo(Name) values('Bob'); mysql> insert into DoesNotEqualDemo(Name) values(''); mysql> insert into DoesNotEqualDemo(Name) values('Larry'); mysql> insert into DoesNotEqualDemo(Name) values(NULL);
使用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)