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 --