03-03-高级操作(运算符)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lucky9322/article/details/85774089

查询中的运算符

算术运算符

+ - * / %

进行基本算术运算:通常不在条件中使用,而是用于结果运算(select字段中)

create table ysf1(
int_1 int,
int_2 int,
int_3 int,
int_4 int
)charset=utf8;

insert into ysf1 values(100,-100,0,default);

-- 算术运算

select int_1+int_2, int_1-int_2, int_1*int_2, int_1/int_2,int_2/int_3,int_2/6,int_4/5 from ysf1;

在这里插入图片描述

比较运算符

> >= < <= = <>

=MySQL中使用该符合进行判断相等 可以用在where后,但不可以用在结果字段中

<=>:也是判断相等的运算符,可以使用在结果字段中

通常是用来在条件中进行限定结果。

select * from my_student where stu_age>=20;

在这里插入图片描述

特殊应用:就是在结果字段中进行比较运算

在这里插入图片描述

在条件判断的时候,还有对应的比较运算符:计算区间

between 条件1 and 条件2

select * from my_student stu_age between 20 and 30;

逻辑运算符

and or not

select * from my_student where stu_age > 20 and stu_age <30;

查找身高超过170的或者年龄大于20岁的学生

select * from my_student where  stu_height >= 170 or stu_age >= 20;

in运算符

in在什么里面,是用来代替=,当结果不是一个值,而是一个结果集的时候。

基本语法:in(结果1,结果2...)

只要当前条件在结果集中出现过,那么就成立

select * from my_student where stu_id in ('stu0001','stu0004','stu0007');

is运算符

is是专门用来判断字段是否为NULL的运算符

基本语法:is null / is not null

查询为null的数据

select * from my_int where int_6 is null;

like运算符

like运算符:用于模糊匹配(匹配字符串)

基本语法:like '匹配模式'

匹配模式中有两种占位符

  • _:匹配对应的单个字符
  • %:匹配多个字符

获取所有姓小的

select * from my_student where stu_name like '小%';

猜你喜欢

转载自blog.csdn.net/lucky9322/article/details/85774089
今日推荐