萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> MySQL 常用的運算符操作實例

MySQL 常用的運算符操作實例

運算符優先級順序
最高優先級 :=
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

算數運算符

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

 

邏輯運算符

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

比較運算符
等於
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';


位運算符
位與
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;

copyright © 萬盛學電腦網 all rights reserved