MySQL学习笔记(5)--运算符

·算术运算符

通常用于结果运算(select 字段中)

create table my_int(
    a int,
    b int,
    c int,
    d int 
);
insert into my_int values(1,-1,0,default);
select a + b, a - b, a * b, a / b, a / c, d / b from my_int;
--分母为0时结果是NULL,NULL进行任何计算结果都是NULL

·比较运算符

‘> >= < <= = <>(不等于)’
=:在MySQL中没有==,也可以用<=>来比较
特殊应用:在字段结果在进行比较运行

select '1' <=> 1, 0.02 <=> 0, 0.02 <> 0;

在这里插入图片描述
在条件判断的时候还有对应的比较计算符:区间

between 条件1 and 条件2   --闭区间查找,条件1必须小于条件2,反过来查不到信息
--实例
select * from my_student where stu_age between 20 and 30; 

·逻辑运算符

1. and与 or或 not非

select * from my_student where stu_age >= 20 and stu_age <= 30;  --结果同上

2. ·in
用来替代 = ,当结果不是一个值,而是结果集的时候

in(结果1, 结果2....)
--实例
select * from my_student where stu_id in ('stu001','stu002');

3. is

is null / is not null
--实例
select * from my_int where d = null;  --没有结果,因为null进行任何运算结果都是null
select * from my_int where d is null;  --成功

专用于判断字段是否为NULL
4. like
用于模糊匹配, 用到 _ 和 %

发布了19 篇原创文章 · 获赞 20 · 访问量 9550

猜你喜欢

转载自blog.csdn.net/qq_39323164/article/details/104120698
今日推荐