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 |
-- 数值比较 select * from employees where salary = 5000; -- 字符串比较 select * from products where name = 'laptop';
=
相同-- null值安全比较 select * from customers where phone_number <=> null; -- 常规比较 select * from orders where status <=> 'completed';
<>
是标准sql语法!=
是兼容性语法-- 使用<> select * from students where grade <> 'f'; -- 使用!= select * from inventory where stock_quantity != 0;
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
→ truenull <= 5
→ null-- 年龄筛选 select * from users where age <= 18; -- 库存检查 select * from warehouse where quantity <= reorder_level;
'apple' > 'banana'
→ false(按字符编码比较)-- 薪资查询 select name from employees where salary > 10000; -- 时间范围 select * from logs where timestamp > '2023-06-01 00:00:00';
-- 分数筛选 select student_id from exams where score >= 60; -- 日期范围 select * from reservations where end_date >= '2023-12-31';
运算符 | 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
是错误语法)-- 查找未填写电话号码的用户 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
-- 获取最早日期 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
-- 查找特定品类商品 select * from products where category_id in (1, 3, 5);
7.not in
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 比较运算符内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论