符号类型运算符
运算符 | 名称 | 作用 | 示例 |
---|---|---|---|
= | 等于运算符 | 判断两个值、字符串或表达式是否相等 | 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
→ truenull <= 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
是错误语法) - 适用于所有数据类型
- 唯一正确的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 比较运算符内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论