11 MySQL 运算符

MySQL 运算符可分为4大类: 1 算术运算符; 2 比较运算符; 3 逻辑运算符; 4 位运算符

1 算术运算符

符号说明
+
-
*
/ 或 DIV
% 或 MOD

示例,及执行结果如下:

select store, store+1, store-1, store/2, store*2, store%2 from bookinfo;
storestore+1store-1store/2store*2store%2
5642.5000101
2311.000040
3421.500061
101195.0000200
5642.5000101
1200.500021
4532.000080

2 比较运算符

符号说明
= , > , <等于, 大于 , 小于
<> 或 !=不等于
<= , >=小于等于, 大于等于
IS [NOT] NULL为空/ 不为空
[NOT] BETWEEN … AND …不在/在 两值之间
[NOT] IN不在/在 集合中
<=>严格比较2个NULL值是否相等
2个操作码均为NULL时,其结果为1; 仅1个操作码为NULL时, 其结果为0
LIKE模糊匹配
REGEXP 或 RLIKE正则式匹配

示例代码:

select * from readerinfo where balance>200;  /* 查读者信息表,余额>200的读者信息 */
select * from readerinfo where age is not null;  /* 查读者信息表,年龄不为空的读者信息 */
select * from readerinfo where balance between 350 and 450; /*查读者信息表,余额350至450间的读者*/
select * from readerinfo where name in('张三','李四','王五'); /*查读者信息表,获得:张三/李四/王五*/

select * from readerinfo where name like '张_'; /* 查读者信息表,2个字的姓[张]读者 */
select * from readerinfo where name like '133%'; /* 查读者信息表,手机号以133开头的读者信息 */

注:① 下划线( _ )1个字符占位符; ② 百分号( % )多个字符占位符;

3 逻辑运算符

逻辑运算符共3个: and 且 / or 或 / not 非

-- 查询图书信息表, 价格price>50 且 库存store<5的信息
select * from bookinfo where price>50 and store<5;

-- 图书信息表, 查询price>80或 出版社=机械工业出版社
select * from bookinfo where price>80 or press='机械工业出版社';

-- 图书信息表, price不在50~100区间的
select * from bookinfo where price not between 50 and 100;

4 位运算符

符号说明
&按位与
按位或
^按位异或
!取反
<<左移
>>右移

*5 运算符优先级

优先级运算符
1低:=
2
3&&, and
4not
5between, case, when, then, else
6=, <=>, >=, >, <=, <, !=, <>, is, like, regexp, in
7
8&
9<<, >>
10-, +
11*, /, div, %, mod
12^
13- (一元减号), ~(一元比特反转)
14高!, binary, collate
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值