MySQL 支持的算術運算符包括加、減、乘、除和模運算。正則表達式
運算符 | 做用 |
---|---|
+ | 加法,得到一個或多個值的和 |
- | 減法,從一個值中減去另外一個值 |
* | 乘法,獲得兩個或多個值的乘積 |
/,div | 商運算,用一個值除以另外一個值獲得商 |
%,mod | 模運算,用一個值除以另一個值獲得餘數 |
1)在除法運算和模運算中,若是除數爲 0,將是非法除數,返回結果爲 NULL。安全
> select 1/0, 100%0; +------+-------+ | 1/0 | 100%0 | +------+-------+ | NULL | NULL | +------+-------+ 1 row in set, 2 warnings (0.00 sec)
2)對於除法運算,還有另外一種表達方式,使用 a div b 函數,從除法結果中捨棄小數點右側的小數部分。函數
> select 5/2, 5 div 2; +--------+---------+ | 5/2 | 5 div 2 | +--------+---------+ | 2.5000 | 2 | +--------+---------+ 1 row in set (0.00 sec)
3)對於模運算,還有另外一種表達方式,使用 mod(a,b) 函數與 a%b 效果同樣。code
> select 5%2, mod(5, 2); +------+-----------+ | 5%2 | mod(5, 2) | +------+-----------+ | 1 | 1 | +------+-----------+ 1 row in set (0.00 sec)
當使用 select 語句進行查詢時, MySQL 容許用戶對錶達式的左邊操做數和右邊操做數進行比較,比較結果爲真,則返回 1,爲假則返回 0,比較結果不肯定則返回 null。regexp
比較運算符能夠用於比較數字、字符串和表達式。數字做爲浮點數比較,而字符串以不區分大小寫的方式進行比較。字符串
運算符 | 做用 |
---|---|
= | 等於,用於比較運算符兩側的操做數是否相等,相等返回 1,不然爲 0,null 不能用 "=" 比較 |
<>、!= | 不等於,和 "=" 相反,不等返回 1,不然爲 0,null 不能用 "<>" 比較 |
<=> | null 安全的等於 (null-safe),和 "=" 相似,相等返回 1,能夠比較 null |
< | 小於 |
<= | 小於等於 |
> | 大於 |
>= | 大於等於 |
between | 存在於指定範圍,a BETWEEN min AND max,當 a 大於等於 min 而且小於等於 max,則返回值爲 1,不然返回0;操做數 a、min、max 類型相同時,此表達式等價於(a >= min and a <= max),當操做數類型不一樣時,比較時會遵循類型轉換原則進行轉換後,再進行比較運算 |
in | 存在於指定集合,a IN (value1, value2, …),當 a 的值存在於列表中時,則整個比較表達式返回的值爲 1,不然返回 0 |
is null | 爲 null,a IS NULL,當 a 的值爲 null,則返回值爲 1,不然返回 0 |
is not null | 不爲 null,a IS NOT NULL,和 "is null" 相反,當 a 的值不爲 NULL,則返回值爲 1,不然返回 0 |
like | 通配符匹配,a LIKE %123%,當 a 中含有字符串 "123" 時,則返回值爲 1,不然返回 0 |
regexp、rlike | 正則表達式,str REGEXP str_pat,當 str 字符串中含有 str_pat 相匹配的字符串時,則返回值爲 1,不然返回 0 |
示例it
> select 1 = 0, 1 = 1, NULL = NULL; +-------+-------+-------------+ | 1 = 0 | 1 = 1 | NULL = NULL | +-------+-------+-------------+ | 0 | 1 | NULL | +-------+-------+-------------+ 1 row in set (0.00 sec) > select 1 = 0, 1 = 1, NULL = NULL; +-------+-------+-------------+ | 1 = 0 | 1 = 1 | NULL = NULL | +-------+-------+-------------+ | 0 | 1 | NULL | +-------+-------+-------------+ 1 row in set (0.00 sec) > select 1 <=> 1, 2 <=> 0, 0 <=> 0, NULL <=> NULL; +---------+---------+---------+---------------+ | 1 <=> 1 | 2 <=> 0 | 0 <=> 0 | NULL <=> NULL | +---------+---------+---------+---------------+ | 1 | 0 | 1 | 1 | +---------+---------+---------+---------------+ 1 row in set (0.00 sec) > select 'a '< 'b', 'a' < 'a', 'a' < 'c', 1 < 2; +-----------+-----------+-----------+-------+ | 'a '< 'b' | 'a' < 'a' | 'a' < 'c' | 1 < 2 | +-----------+-----------+-----------+-------+ | 1 | 0 | 1 | 1 | +-----------+-----------+-----------+-------+ 1 row in set (0.00 sec) > select 'bdf' <= 'b', 'b' <= 'b', 0 < 1; +--------------+------------+-------+ | 'bdf' <= 'b' | 'b' <= 'b' | 0 < 1 | +--------------+------------+-------+ | 0 | 1 | 1 | +--------------+------------+-------+ 1 row in set (0.00 sec) > select 'a' > 'b', 'abc' > 'a', 1 > 0; +-----------+-------------+-------+ | 'a' > 'b' | 'abc' > 'a' | 1 > 0 | +-----------+-------------+-------+ | 0 | 1 | 1 | +-----------+-------------+-------+ 1 row in set (0.00 sec) > select 'a' >= 'b', 'abc' >= 'a', 1 >= 0, 1 >= 1; +------------+--------------+--------+--------+ | 'a' >= 'b' | 'abc' >= 'a' | 1 >= 0 | 1 >= 1 | +------------+--------------+--------+--------+ | 0 | 1 | 1 | 1 | +------------+--------------+--------+--------+ 1 row in set (0.00 sec) > select 10 between 10 and 20, 9 between 10 and 20; +----------------------+---------------------+ | 10 between 10 and 20 | 9 between 10 and 20 | +----------------------+---------------------+ | 1 | 0 | +----------------------+---------------------+ 1 row in set (0.00 sec) > select 1 in (1, 2, 3), 't' in ('t', 'a', 'b', 'l', 'e'), 0 in (1, 2); +----------------+----------------------------------+-------------+ | 1 in (1, 2, 3) | 't' in ('t', 'a', 'b', 'l', 'e') | 0 in (1, 2) | +----------------+----------------------------------+-------------+ | 1 | 1 | 0 | +----------------+----------------------------------+-------------+ 1 row in set (0.00 sec) > select 0 is null, null is null; +-----------+--------------+ | 0 is null | null is null | +-----------+--------------+ | 0 | 1 | +-----------+--------------+ 1 row in set (0.00 sec) > select 0 is not null, null is not null; +---------------+------------------+ | 0 is not null | null is not null | +---------------+------------------+ | 1 | 0 | +---------------+------------------+ 1 row in set (0.00 sec) > select 123456 like '123%', 123456 like '%123%', 123456 like '%321%'; +--------------------+---------------------+---------------------+ | 123456 like '123%' | 123456 like '%123%' | 123456 like '%321%' | +--------------------+---------------------+---------------------+ | 1 | 1 | 0 | +--------------------+---------------------+---------------------+ 1 row in set (0.00 sec) > select 'abcdef' regexp 'ab', 'abcdefg' regexp 'k'; +----------------------+----------------------+ | 'abcdef' regexp 'ab' | 'abcdefg' regexp 'k' | +----------------------+----------------------+ | 1 | 0 | +----------------------+----------------------+ 1 row in set (0.01 sec)
邏輯運算符又稱爲布爾運算符,用來確認表達式的真和假。io
運算符 | 做用 |
---|---|
not、! | 邏輯非,返回和操做數相反的結果:0(假)返回值爲 1,不然值爲 0。NOT NULL 的返回值爲 NULL |
and、&& | 邏輯與,當全部操做數均爲非零值而且不爲 NULL 時,計算所得結果爲 1,當一個或多個操做數爲 0 時,所得結果爲 0,操做數中有任何一個爲 NULL 則返回值爲 NULL |
or、|| | 邏輯或,當兩個操做數均爲非 NULL 值時,若有任意一個操做數爲非零值,則結果爲 1,不然結果爲 0。當有一個操做數爲 NULL 時,如另外一個操做數爲非零值,則結果爲 1,不然結果爲 NULL。假如兩個操做數均爲 NULL,則所得結果爲 NULL |
xor | 邏輯異或,任意一個操做數爲 NULL 時,返回值爲 NULL。對於非 NULL 的操做數,若是兩個的邏輯真假值相異,則返回結果 1,不然返回 0 |
示例table
> select not 0, not 1, not null; +-------+-------+----------+ | not 0 | not 1 | not null | +-------+-------+----------+ | 1 | 0 | NULL | +-------+-------+----------+ 1 row in set (0.00 sec) > select 1 and 1, 0 and 1, 3 and 1, 1 and null; +---------+---------+---------+------------+ | 1 and 1 | 0 and 1 | 3 and 1 | 1 and null | +---------+---------+---------+------------+ | 1 | 0 | 1 | NULL | +---------+---------+---------+------------+ 1 row in set (0.00 sec) > select 1 or 0, 0 or 0, 1 or null, 1 or 1, null or null; +--------+--------+-----------+--------+--------------+ | 1 or 0 | 0 or 0 | 1 or null | 1 or 1 | null or null | +--------+--------+-----------+--------+--------------+ | 1 | 0 | 1 | 1 | NULL | +--------+--------+-----------+--------+--------------+ 1 row in set (0.00 sec) > select 1 xor 1, 0 xor 0, 1 xor 0, 0 xor 1, null xor 1; +---------+---------+---------+---------+------------+ | 1 xor 1 | 0 xor 0 | 1 xor 0 | 0 xor 1 | null xor 1 | +---------+---------+---------+---------+------------+ | 0 | 0 | 1 | 1 | NULL | +---------+---------+---------+---------+------------+ 1 row in set (0.00 sec)
位運算是將給定的操做數轉化爲二進制後,對各個操做數每一位都進行指定的邏輯運算,獲得的二進制結果轉換爲十進制數後就是位運算的結果。class
運算符 | 做用 |
---|---|
& | 位與(位 and),對多個操做數的二進制位作邏輯與操做 |
| | 位或(位 or),對多個操做數的二進制位作邏輯或操做 |
^ | 位異或(位 xor),對操做數的二進制位作異或操做 |
~ | 位取反,對操做數的二進制位作 not 操做,這裏的操做數只能是一位 |
>> | 位右移,左操做數向右移動右操做數指定的位數,左邊補 0 |
<< | 位左移,右操做數向左移動左操做數指定的位數。右邊補 0 |
示例
> select 2&3&4; +-------+ | 2&3&4 | +-------+ | 0 | +-------+ 1 row in set (0.00 sec) > select 2|3; +-----+ | 2|3 | +-----+ | 3 | +-----+ 1 row in set (0.00 sec) > select 2^3; +-----+ | 2^3 | +-----+ | 1 | +-----+ > select ~1 , ~18446744073709551614; +----------------------+------------------------+ | ~1 | ~ 18446744073709551614 | +----------------------+------------------------+ | 18446744073709551614 | 1 | +----------------------+------------------------+ 1 row in set (0.00 sec) > select 100 >> 3; +----------+ | 100 >> 3 | +----------+ | 12 | +----------+ 1 row in set (0.00 sec) > select 100 << 3; +----------+ | 100 << 3 | +----------+ | 800 | +----------+ 1 row in set (0.00 sec)
優先級順序
優先級順序 | 運算符 |
---|---|
1 | := |
2 | ||, OR, XOR |
3 | &&, AND |
4 | NOT |
5 | BETWEEN, CASE, WHEN, THEN, ELSE |
6 | =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN |
7 | | |
8 | & |
9 | <<, >> |
10 | -, + |
11 | *, /, DIV, %, MOD |
12 | ^ |
13 | - (unary minus), ~ (unary bit inversion) |
14 | ! |
實際上,不多有人能將這些優先級熟練記憶,不少狀況下咱們都是用 "()" 來將須要優先的操做括起來,這樣既起到了優先的做用,又使得其餘用戶看起來更易於理解。