# 【实验】【MySQL】MySQL中的运算符展示

1.算数运算符

mysql> select 1+2;

mysql> select 2-1;

mysql> select 2*3;

mysql> select 5/3;

mysql> SELECT 5 DIV 2;

mysql> select 5%2,mod(5,2);

2.比较运算符

mysql> select 1=0,1=1,null=null;

mysql> select 1<>0,1<>1,null<>null;

mysql> select 1<=>1,2<=>0,0<=>0,null<=>null;

mysql> select 'a'<'b','a'<'a','a'<'c',1<2;

mysql> select 'bdf'<='b','b'<='b',0<1;

mysql> select 'a'>'b','abc'>'a',1>0;

mysql> select 'a'>='b','abc'>='a',1>=0,1>=1;
BETWEEN
mysql> select 10 between 10 and 20, 9 between 10 and 20;
IN
mysql> select 1 in (1,2,3), 't' in ('t','a','b','l','e'), 0 in (1,2);
IS NULL
mysql> select 0 is null,null is null;
IS NOT NULL
mysql> select 0 is not null, null is not null;
LIKE
mysql> select 123456 like '123%', 123456 like '%123%', 123456 like '%321%';
REGEXP
mysql> select 'abcdef' regexp 'ab', 'abcdefg' regexp 'k';

3.逻辑运算符

mysql> select not 0, not 1, not null;
mysql> select ! 0, ! 1, ! null;

mysql> select (1 and 1), (0 and 1), (3 and 1), (1 and null);
mysql> select (1 && 1), (0 && 1), (3 && 1), (1 && null);

mysql> select (1 or 0), (0 or 0), (1 or null), (1 or 1), (null or null);
mysql> select (1 || 0), (0 || 0), (1 || null), (1 || 1), (null || null);

mysql> select (1 xor 1), (0 xor 0), (1 xor 0), (0 xor 1), (null xor 1);
mysql> select (1 ^ 1), (0 ^ 0), (1 ^ 0), (0 ^ 1), (null ^ 1);

4.位运算符

mysql> select 2&3;
mysql> select 2&3&4;

mysql> select 2|3;

mysql> select 2^3;

mysql> select ~1,~18446744073709551614;

mysql> select 100>>3;

mysql> select 100<<3;

5.运算符优先级顺序

1            ||, OR, XOR
2            &&, AND
3            BETWEEN, CASE, WHEN, THEN, ELSE
4            =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
5            |
6            &
7            <<, >>
8            -, +
9            *, /, DIV, %, MOD
10           ^
11           - (unary minus), ~ (unary bit inversion)
12           !, NOT

-- The End --

Oracle ACE 总监，阿里云MVP，北京大学理学硕士，恩墨学院创始人，教育专家，中国区 Cloudera 首位官方授权大数据讲师，金牌培训专家，BDA大数据联盟创始人，OCM联盟创始人，ACCUG创始人、ACOUG核心专家，Blogger。

• 博文量
797
• 访问量
7995756