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
最低优先级 BINARY, COLLATE
好运。
-- The End --
座右铭:纸上得来终觉浅,绝知此事要躬行!
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
最低优先级 BINARY, COLLATE
好运。
-- The End --