MySQL 不等于的三种使用及区别

mysql中常用到判断符号,而不等于是比较常用的符号,下面讲解以下三种不等于符号

符号 意义
<> 不等于的最早用法,可移植性优于下面两种
!= 后来mysql添加上的,类似于java等编程语言中的不等于
not in not in后面加上数据,表示不在该数据里面

mysql中推荐使用<>来表示不等于,为什么呢?因为可移植性强,因为查询速度快。在leetcode上有一道题,是电影院查询的题目,题目如下:

其实非常简单,查询description非boring并且id非偶数的,将查询结果利用order by进行排序即可,但在查询description非boring的时候要用到不等于来判断,下面就是我使用三种不等于的查询时间的比拼

可以看出来<>还是快一些的,所以还是推荐使用<>来表示不等于的

多说无益,来个实例!!!

一个简单地表数据:

select * from user where address != "北京"

select * from user where address <> "北京"

select * from user where address = null

select * from user where address is null

select * from user where address != null

总结:

select * from user where address != "北京"
 
select * from user where address <> "北京"
 
select * from user where address = null
select * from user where address is null
 
select * from user where address != null
select * from user where address is not null

短短几条语句,三个极其常见的点,或许我们在回答的时候却不知所措,犹豫不决。

在<>和!=是等价的。在某字段不等于某值(非空的值)时,输出的结果此字段为空不输出。

is 和 is not 用于和 null 结合,我称它为不是,不是空

到此这篇关于mysql 不等于的三种使用及区别的文章就介绍到这了,更多相关mysql 不等于内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐