it编程 > 数据库 > Mysql

MySQL中比较运算符的具体使用

17人参与 2025-07-17 Mysql

符号类型运算符

运算符名称作用示例
=等于运算符判断两个值、字符串或表达式是否相等select * from users where age = 25
select name from products where category = 'electronics'
<=>安全等于运算符安全地判断两个值、字符串或表达式是否相等(兼容null值)select * from employees where salary <=> null
select * from orders where coupon_code <=> 'discount'
<>不等于运算符判断两个值、字符串或表达式是否不相等select * from students where gender <> 'f'
select id from logs where status <> 200
!=不等于运算符判断两个值、字符串或表达式是否不相等select * from inventory where quantity != 0
select email from users where deleted_at != null
<小于运算符判断前面的值是否小于后面的值select * from products where price < 100
select * from events where start_time < '2023-01-01'
<=小于等于运算符判断前面的值是否小于等于后面的值select * from members where age <= 18
select * from tasks where priority <= 3
>大于运算符判断前面的值是否大于后面的值select * from employees where salary > 5000
select * from articles where views > 1000
>=大于等于运算符判断前面的值是否大于等于后面的值select * from candidates where score >= 60
select * from reservations where guests >= 4

1. 等于运算符=

2. 安全等于运算符<=>

3. 不等于运算符<>或!=

4. 小于运算符<

5. 小于等于运算符<=

6. 大于运算符>

7. 大于等于运算符>=

8.综合比较表

运算符null处理适用场景典型用例
=返回null精确匹配查询用户登录验证
<=>返回true需要包含null值的比较可选字段检查
<>/!=返回null排除特定值的查询过滤无效记录
<返回null范围查询(开区间)查找历史数据
<=返回null范围查询(闭区间)统计截止某日期的数据
>返回null下限筛选查找高价值客户
>=返回null下限筛选(含边界)达标数据筛选

非符号类型运算符

运算符名称作用示例
is null为空运算符判断值/字符串/表达式是否为空select b from table where a is null
is not null不为空运算符判断值/字符串/表达式是否不为空select b from table where a is not null
least最小值运算符在多个值中返回最小值select d from table where c = least(a,b)
greatest最大值运算符在多个值中返回最大值select d from table where c = greatest(a,b)
between区间运算符判断值是否在两个值之间select d from table where c between a and b
in属于运算符判断值是否为列表中的任意一个select d from table where c in (a,b)
not in不属于运算符判断值是否不在列表中select d from table where c not in (a,b)
like模糊匹配运算符判断值是否符合模糊匹配规则select c from table where a like b
regexp/rlike正则表达式运算符判断值是否符合正则表达式select c from table where a regexp b

一、空值判断运算符

1.is null

2.is not null

二、极值运算符

3.least

4.greatest

三、范围运算符

5.between

四、集合运算符

6.in

7.not in

五、模式匹配运算符

8.like

9.regexp/rlike

六、特殊说明

10.isnull(mysql特有)

到此这篇关于mysql中比较运算符的具体使用的文章就介绍到这了,更多相关mysql 比较运算符内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

您想发表意见!!点此发布评论

推荐阅读

虚拟机Centos7安装MySQL数据库实践

07-17

MySQL中WITH ROLLUP的具体使用

07-17

MYSQL中慢SQL原因与优化方法详解

07-17

一文详解为什么用ElasticSearch以及和传统数据库MySQL有什么区别

07-17

MySQL查询优化与事务实战教程

07-18

MySQL 数据库空间使用大小查询的方法实现

07-18

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论