当前位置: 代码网 > it编程>数据库>Mysql > MySQL中比较运算符的具体使用

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

2025年07月17日 Mysql 我要评论
符号类型运算符运算符名称作用示例=等于运算符判断两个值、字符串或表达式是否相等select * from users where age = 25select name from products w

符号类型运算符

运算符名称作用示例
=等于运算符判断两个值、字符串或表达式是否相等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. 等于运算符=

  • 作用:判断两个值、字符串或表达式是否相等
  • 特点
    • 严格比较(null = null 返回null)
    • 区分大小写(取决于数据库配置)
  • 示例
    -- 数值比较
    select * from employees where salary = 5000;
    
    -- 字符串比较
    select * from products where name = 'laptop';
    

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

  • 作用:安全地判断两个值/字符串/表达式是否相等(含null值)
  • 特点
    • null <=> null 返回true
    • 其他情况与 = 相同
  • 示例
    -- null值安全比较
    select * from customers where phone_number <=> null;
    
    -- 常规比较
    select * from orders where status <=> 'completed';
    

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

  • 作用:判断两个值/字符串/表达式是否不相等
  • 特点
    • <> 是标准sql语法
    • != 是兼容性语法
    • 两者功能完全相同
  • 示例
    -- 使用<>
    select * from students where grade <> 'f';
    
    -- 使用!=
    select * from inventory where stock_quantity != 0;
    

4. 小于运算符<

  • 作用:判断前面的值是否小于后面的值
  • 数据类型支持
    • 数值:3 < 5 → true
    • 字符串:按字典序比较
    • 日期:'2023-01-01' < '2023-02-01' → true
  • 示例
    -- 数值比较
    select * from products where price < 1000;
    
    -- 日期比较
    select * from events where start_date < current_date;
    

5. 小于等于运算符<=

  • 作用:判断前面的值是否小于或等于后面的值
  • 边界情况
    • 5 <= 5 → true
    • null <= 5 → null
  • 示例
    -- 年龄筛选
    select * from users where age <= 18;
    
    -- 库存检查
    select * from warehouse where quantity <= reorder_level;
    

6. 大于运算符>

  • 作用:判断前面的值是否大于后面的值
  • 字符串比较规则
    • 'apple' > 'banana' → false(按字符编码比较)
    • 大小写敏感(‘a’ > ‘a’ 结果取决于排序规则)
  • 示例
    -- 薪资查询
    select name from employees where salary > 10000;
    
    -- 时间范围
    select * from logs where timestamp > '2023-06-01 00:00:00';
    

7. 大于等于运算符>=

  • 作用:判断前面的值是否大于或等于后面的值
  • 特殊注意
    • 对null值的比较总是返回null
    • 适用于范围查询的闭合区间
  • 示例
    -- 分数筛选
    select student_id from exams where score >= 60;
    
    -- 日期范围
    select * from reservations where end_date >= '2023-12-31';
    

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

  • 功能:判断值/字符串/表达式是否为空
  • 语法where column is null
  • 特点
    • 唯一正确的null值判断方式(= null是错误语法)
    • 适用于所有数据类型
  • 示例
    -- 查找未填写电话号码的用户
    select * from users where phone is null;
    

2.is not null

  • 功能:判断值/字符串/表达式是否不为空
  • 语法where column is not null
  • 应用场景
    • 数据完整性检查
    • 有效记录筛选
  • 示例
    -- 查找已激活的用户(email不为空)
    select * from accounts where email is not null;
    

二、极值运算符

3.least

  • 功能:返回参数列表中的最小值
  • 特点
    • 支持2个及以上参数
    • 可比较数值/字符串/日期等类型
  • 示例
    -- 获取最早日期
    select least('2023-01-01', '2022-12-31', '2023-05-15');
    

4.greatest

  • 功能:返回参数列表中的最大值
  • 典型应用
    • 价格上限控制
    • 有效期计算
  • 示例
    -- 计算最终价格(不超过建议零售价)
    select product_id, least(price, msrp) as final_price from products;
    

三、范围运算符

5.between

  • 功能:判断值是否在指定范围内(闭区间)
  • 等效写法where x >= a and x <= b
  • 注意事项
    • 总是先写小值再写大值
    • 日期范围查询时包含边界
  • 示例
    -- 查询2023年q2订单
    select * from orders 
    where order_date between '2023-04-01' and '2023-06-30';
    

四、集合运算符

6.in

  • 功能:判断值是否在指定列表中
  • 性能建议
    • 列表项超过100时考虑临时表
    • 支持子查询
  • 示例
    -- 查找特定品类商品
    select * from products 
    where category_id in (1, 3, 5);
    

7.not in

  • 功能:判断值是否不在指定列表中
  • null值陷阱
    • 当列表包含null时,所有比较返回null
    • 建议配合is not null使用
  • 示例
    -- 排除测试账户
    select * from users 
    where user_id not in (999, 1000);
    

五、模式匹配运算符

8.like

  • 通配符
    符号功能
    %匹配任意数量字符
    _匹配单个字符
  • 大小写敏感:取决于数据库配置
  • 示例
    -- 查找j开头的姓名
    select * from employees 
    where name like 'j%';
    

9.regexp/rlike

  • 功能:正则表达式匹配(两者完全等效)
  • 常用模式
    • ^:字符串开始
    • $:字符串结束
    • [0-9]:数字字符
  • 示例
    -- 验证邮箱格式
    select * from contacts 
    where email regexp '^[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\.[a-za-z]{2,}$';
    

六、特殊说明

10.isnull(mysql特有)

  • 注意:与is null功能相同,但非sql标准
  • 建议:优先使用标准语法is null

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

(0)

相关文章:

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

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com