程序員需掌握的SQL運算符(三)

1.前言

Mysql 支持多種類型的運算符,運算符可以爲操作數進行運算。本文從Mysql 5.7版本出發,下面將詳細介紹幾種常見的運算符。Mysql運算符主要有四大類,它們分別是:算術運算符比較運算符邏輯運算符位操作運算符

2 算術運算符

算術運算符包括加(+)、減(—)、乘(*)、除(/)、模運算(%)

下面簡單演示這幾種運算符的使用如下:

  • 創建表
mysql> create table computeDemo(num int not null);
Query OK, 0 rows affected (0.01 sec)
  • 插入數據
mysql> insert into computeDemo values(16),(32);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

接下來我們對num的值進行加法和法、減法、乘法、除法、取模運算。

mysql> update  computeDemo set num= num-4 where num=16;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from computeDemo;
+-----+
| num |
+-----+
|  12 |
|  32 |
+-----+
2 rows in set (0.00 sec)

mysql> update computeDemo set num=num+8 where num=32;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from computeDemo;
+-----+
| num |
+-----+
|  12 |
|  40 |
+-----+
2 rows in set (0.00 sec)

mysql> update computeDemo set num=num*2 where num=12;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from computeDemo;
+-----+
| num |
+-----+
|  24 |
|  40 |
+-----+
2 rows in set (0.00 sec)

mysql> update computeDemo set num=num/2 where num=40;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from computeDemo;
+-----+
| num |
+-----+
|  24 |
|  20 |
+-----+
2 rows in set (0.00 sec)

mysql> update computeDemo set num=num%3 where num=20;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from computeDemo;
+-----+
| num |
+-----+
|  24 |
|   2 |
+-----+
2 rows in set (0.00 sec)

3. 比較運算符

比較運算符常用於SELECT語句查詢中,下表列出了Mysql中常見的比較運算符:

運算符 解釋說明
= 或 <=> 判斷左右兩方值是否相等或者兩方安全等於
< 或 > 判斷小於或者大於
<= 或 >= 判斷小於等於或者大於等於
!= 判斷不想等於
BETWEEN AND 判斷值是否在指定區域之間
IS NULL 或 IS NOT NULL 判斷值爲null或者不爲null
IN 或 NOT IN 判斷值位於指定集合內或不在某個集合中
LIKE 通配符匹配
REGEXP 正則表達式匹配
  • 等於運算符與安全等於

等於運算符可以比較運算符兩側值是否相等,但是需要注意的是NULL不能用於等於比較運算符中。如果值相等則返回1 否則返回0。

mysql> select 1=0,1=1,NULL=2;
+-----+-----+--------+
| 1=0 | 1=1 | NULL=2 |
+-----+-----+--------+
|   0 |   1 |   NULL |
+-----+-----+--------+
1 row in set (0.00 sec)

對於NULL值的比較可以使用<=>如下所示:

mysql> select 1=0,1=1,NULL<=>2;
+-----+-----+----------+
| 1=0 | 1=1 | NULL<=>2 |
+-----+-----+----------+
|   0 |   1 |        0 |
+-----+-----+----------+
1 row in set (0.00 sec)
  • 大於或者小於
mysql> select 5>1,5<1;
+-----+-----+
| 5>1 | 5<1 |
+-----+-----+
|   1 |   0 |
+-----+-----+
1 row in set (0.00 sec)
  • 大於等於或小於等於
mysql> select 5>=5,2<=3;
+------+------+
| 5>=5 | 2<=3 |
+------+------+
|    1 |    1 |
+------+------+
1 row in set (0.00 sec)
  • 不等於
mysql> select 5!=5,5=5;
+------+-----+
| 5!=5 | 5=5 |
+------+-----+
|    0 |   1 |
+------+-----+
1 row in set (0.00 sec)
  • between and

between and 運算格式爲:a betwwen min and max ,此與a>= min && a<=max 等價。

mysql> select 5 between 1 and 10;
+--------------------+
| 5 between 1 and 10 |
+--------------------+
|                  1 |
+--------------------+
1 row in set (0.01 sec)
  • is null 與 is not null

判斷某個值爲 null 或者不爲 null

mysql> select null is null, null is not null;
+--------------+------------------+
| null is null | null is not null |
+--------------+------------------+
|            1 |                0 |
+--------------+------------------+
1 row in set (0.00 sec)
  • in 與 not in
mysql> select 4 in (1,3,4,5);
+----------------+
| 4 in (1,3,4,5) |
+----------------+
|              1 |
+----------------+
1 row in set (0.00 sec)

mysql> select 2 in (1,3,4,5);
+----------------+
| 2 in (1,3,4,5) |
+----------------+
|              0 |
+----------------+
1 row in set (0.00 sec)
  • like

like查詢在一般sql查詢非常多,請看下面一個查詢的案列如下所示:

1、 LIKE’Mc%’ 將搜索以字母 Mc 開頭的所有字符串(如 McBadden )。

2 、 LIKE’%inger’ 將搜索以字母 inger 結尾的所有字符串(如 Ringer 、 Stringer )。

3 、 LIKE’%en%’ 將搜索在任何位置包含字母 en 的所有字符串(如 Bennet 、 Green 、 McBadden )。

4 、 LIKE’_heryl’ 將搜索以字母 heryl 結尾的所有六個字母的名稱(如 Cheryl 、 Sheryl )。 _代表一個字符

5 、 LIKE’[CK]ars[eo]n’ 將搜索下列字符串: Carsen 、 Karsen 、 Carson 和 Karson (如 Carson )。

6 、 LIKE’[M-Z]inger’ 將搜索以字符串 inger 結尾、以從 M 到 Z 的任何單個字母開頭的所有名稱(如 Ringer )。

7 、 LIKE’M[^c]%’ 將搜索以字母 M 開頭,並且第二個字母不是 c 的所有名稱(如 MacFeather )。

  • regexp

正則匹配查詢在日常開發中應用的比較少,下面將一個簡單例子演示其使用

mysql> select 'abc' regexp '^a';
+-------------------+
| 'abc' regexp '^a' |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.00 sec)

mysql> select 'abc' regexp '^b';
+-------------------+
| 'abc' regexp '^b' |
+-------------------+
|                 0 |
+-------------------+
1 row in set (0.00 sec)

4. 邏輯運算符

在日常開發中CRUD都大量會應用到邏輯運算符,下面列出了Mysql中使用到的邏輯運算符號

運算符名稱 解釋說明
NOT 或 ! 邏輯非既判斷否
AND 或 && 邏輯與既判斷表達式都爲真
OR 或 || 邏輯或
XOR 邏輯異或
  • NOT 或 !

當操作數爲0的時候,返回值爲1否則返回0,當值爲null則返回null

mysql> select not 0,not 1,not null;
+-------+-------+----------+
| not 0 | not 1 | not null |
+-------+-------+----------+
|     1 |     0 |     NULL |
+-------+-------+----------+
1 row in set (0.01 sec)
  • And 或 &&

當所有的操作數爲非 null 且數值不爲 0

mysql> select (1 and 1),(1 and 0),(1 and null);
+-----------+-----------+--------------+
| (1 and 1) | (1 and 0) | (1 and null) |
+-----------+-----------+--------------+
|         1 |         0 |         NULL |
+-----------+-----------+--------------+
1 row in set (0.00 sec)
  • or 或 ||

當所有的操作數全爲null 且任意一個操作數不爲0,返回1 否則爲0.

mysql> select (1 or 1),(0 or 0),(1 or null);
+----------+----------+-------------+
| (1 or 1) | (0 or 0) | (1 or null) |
+----------+----------+-------------+
|        1 |        0 |           1 |
+----------+----------+-------------+
1 row in set (0.00 sec)
  • xor

當操作數中有null的值則返回null,對於非null的操作數,如果兩個操作數邏輯不相同則返回1 否則返回0

mysql> select (1 xor 1),(0 xor 0),(1 xor null);
+-----------+-----------+--------------+
| (1 xor 1) | (0 xor 0) | (1 xor null) |
+-----------+-----------+--------------+
|         0 |         0 |         NULL |
+-----------+-----------+--------------+
1 row in set (0.00 sec)

5. 位運算符

位運算符對於JAVA程序員最熟悉不過了,但是對於JAVA程序員來說,在日常使用Mysql中很少會使用Mysql的位運算。一般地都是在從查詢Mysql獲得數據,然後在JAVA程序中對數值進行位運算。下面列出了Mysql支持的位運算符

運算符名稱 使用說明
| 位或
& 位與
^ 位異或
<< 位左移
>> 位右移
按位取返

下面以一個簡單的例子演示其使用:

mysql> select 2|3,2&3,2^3,2>>3,2<<3,~2;
+-----+-----+-----+------+------+----------------------+
| 2|3 | 2&3 | 2^3 | 2>>3 | 2<<3 | ~2                   |
+-----+-----+-----+------+------+----------------------+
|   3 |   2 |   1 |    0 |   16 | 18446744073709551613 |
+-----+-----+-----+------+------+----------------------+
1 row in set (0.00 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章