目錄
1,LIKE
以下是 SQL SELECT 語句使用 LIKE 子句從數據表中讀取數據的通用語法:
SELECT field1, field2,...fieldN
FROM table_name
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'
- %:表示任意 0 個或多個字符。可匹配任意類型和長度的字符,有些情況下若是中文,請使用兩個百分號(%%)表示。
- _:表示任意單個字符。匹配單個任意字符,它常用來限制表達式的字符長度語句。
- []:表示括號內所列字符中的一個(類似正則表達式)。指定一個字符、字符串或範圍,要求所匹配對象爲它們中的任一個。
- [^] :表示不在括號所列之內的單個字符。其取值和 [] 相同,但它要求所匹配對象爲指定字符以外的任一個字符。
- 查詢內容包含通配符時,由於通配符的緣故,導致我們查詢特殊字符 “%”、“_”、“[” 的語句無法正常實現,而把特殊字符用 “[ ]” 括起便可正常查詢
MariaDB [mydb]> select * from students;
+-------+-----------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+-----------+------+------+----------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20003 | 小張 | 男 | 15 | painting | 2005-08-14 |
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
| 20005 | 麗麗 | 女 | 15 | painting | 2005-12-01 |
| 20005 | 李明 | 男 | 16 | music | 2004-08-14 |
| 20006 | 張大明 | 男 | 18 | football | 2002-03-05 |
+-------+-----------+------+------+----------+------------+
7 rows in set (0.001 sec)
選擇birthday 是03月份的學生
MariaDB [mydb]> select * from students where birthday like '%03%';
+-------+-----------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+-----------+------+------+----------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20006 | 張大明 | 男 | 18 | football | 2002-03-05 |
+-------+-----------+------+------+----------+------------+
2 rows in set (0.000 sec)
選擇name是2個字且最後一個字是‘明’的學生
MariaDB [mydb]> select * from students where name like '_明';
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20005 | 李明 | 男 | 16 | music | 2004-08-14 |
+-------+--------+------+------+----------+------------+
2 rows in set (0.002 sec)
選擇除了name是2個字且最後一個字是‘明’以外的學生
MariaDB [mydb]> select * from students where birthday not like '%03%';
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20003 | 小張 | 男 | 15 | painting | 2005-08-14 |
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
| 20005 | 麗麗 | 女 | 15 | painting | 2005-12-01 |
| 20005 | 李明 | 男 | 16 | music | 2004-08-14 |
+-------+--------+------+------+----------+------------+
5 rows in set (0.002 sec)
2,ORDER BY
按age升序、降序排列
SELECT field, field2,... [or column] FROM table_name, table_name2,...
ORDER BY field, field2,... ASC[or DESC]
默認是按升序排列 [ASC]
MariaDB [mydb]> select * from students order by age;
+-------+-----------+------+------+------------+------------+
| id | name | sex | age | interest | birthday |
+-------+-----------+------+------+------------+------------+
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20003 | 小張 | 男 | 15 | basketball | 2005-08-14 |
| 20005 | 麗麗 | 女 | 15 | painting | 2005-12-01 |
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20005 | 李明 | 男 | 16 | music | 2004-08-14 |
| 20006 | 張大明 | 男 | 18 | football | 2002-03-05 |
+-------+-----------+------+------+------------+------------+
7 rows in set (0.000 sec)
指定age按降序排列。
MariaDB [mydb]> select * from students order by age desc;
+-------+-----------+------+------+------------+------------+
| id | name | sex | age | interest | birthday |
+-------+-----------+------+------+------------+------------+
| 20006 | 張大明 | 男 | 18 | football | 2002-03-05 |
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20005 | 李明 | 男 | 16 | music | 2004-08-14 |
| 20003 | 小張 | 男 | 15 | basketball | 2005-08-14 |
| 20005 | 麗麗 | 女 | 15 | painting | 2005-12-01 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
+-------+-----------+------+------+------------+------------+
7 rows in set (0.000 sec)
先指定age按升序排列如果age相同brithday再按降序排列。
MariaDB [mydb]> select * from students order by age, birthday desc;
+-------+-----------+------+------+------------+------------+
| id | name | sex | age | interest | birthday |
+-------+-----------+------+------+------------+------------+
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20005 | 麗麗 | 女 | 15 | painting | 2005-12-01 |
| 20003 | 小張 | 男 | 15 | basketball | 2005-08-14 |
| 20006 | 李明 | 男 | 16 | music | 2004-08-14 |
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20007 | 張大明 | 男 | 18 | football | 2002-03-05 |
+-------+-----------+------+------+------------+------------+
7 rows in set (0.001 sec)
3,UNION
操作符用於合併兩個或多個 SELECT 語句的結果集。請注意,UNION 內部的 SELECT 語句必須擁有相同數量的列。列也必須擁有相似的數據類型。同時,每條 SELECT 語句中的列的順序必須相同。
SQL UNION 語法
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
註釋:默認地,UNION 操作符選取不同的值。如果允許重複的值,請使用 UNION ALL。
SQL UNION ALL 語法
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
另外,UNION 結果集中的列名總是等於 UNION 中第一個 SELECT 語句中的列名。
以下2張表
MariaDB [mydb]> select * from students;
+-------+-----------+------+------+------------+------------+
| id | name | sex | age | interest | birthday |
+-------+-----------+------+------+------------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20003 | 小張 | 男 | 15 | basketball | 2005-08-14 |
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
| 20005 | 麗麗 | 女 | 15 | painting | 2005-12-01 |
| 20006 | 李明 | 男 | 16 | music | 2004-08-14 |
| 20007 | 張大明 | 男 | 18 | football | 2002-03-05 |
+-------+-----------+------+------+------------+------------+
7 rows in set (0.002 sec)
MariaDB [mydb]> select * from score;
+-------+-----------+------+----------+--------+
| id | name | math | language | sports |
+-------+-----------+------+----------+--------+
| 20001 | 小明 | 81 | 86 | 93 |
| 20002 | 小紅 | 86 | 86 | 89 |
| 20003 | 小張 | 77 | 83 | 93 |
| 20004 | 露絲 | 88 | 78 | 65 |
| 20005 | 麗麗 | 92 | 94 | 64 |
| 20006 | 李明 | 75 | 78 | 88 |
| 20007 | 張大明 | 54 | 65 | 95 |
+-------+-----------+------+----------+--------+
7 rows in set (0.000 sec)
MariaDB [mydb]> select id,name from students union select id,name from score;
+-------+-----------+
| id | name |
+-------+-----------+
| 20001 | 小明 |
| 20002 | 小紅 |
| 20003 | 小張 |
| 20004 | 露絲 |
| 20005 | 麗麗 |
| 20006 | 李明 |
| 20007 | 張大明 |
+-------+-----------+
7 rows in set (0.002 sec)
MariaDB [mydb]> select id,name from students union all select id,name from score;
+-------+-----------+
| id | name |
+-------+-----------+
| 20001 | 小明 |
| 20002 | 小紅 |
| 20003 | 小張 |
| 20004 | 露絲 |
| 20005 | 麗麗 |
| 20006 | 李明 |
| 20007 | 張大明 |
| 20001 | 小明 |
| 20002 | 小紅 |
| 20003 | 小張 |
| 20004 | 露絲 |
| 20005 | 麗麗 |
| 20006 | 李明 |
| 20007 | 張大明 |
+-------+-----------+
14 rows in set (0.002 sec)
4,AND
MariaDB [mydb]> select * from students where age=16 and interest='music';
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20006 | 李明 | 男 | 16 | music | 2004-08-14 |
+-------+--------+------+------+----------+------------+
1 row in set (0.001 sec)
5,OR
MariaDB [mydb]> select * from students where age=16 or birthday='2007-07-19';
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
| 20006 | 李明 | 男 | 16 | music | 2004-08-14 |
+-------+--------+------+------+----------+------------+
3 rows in set (0.000 sec)
MariaDB [mydb]> select * from students where (age=16 and birthday='2004-08-14') or birthday='2007-07-19';
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
| 20006 | 李明 | 男 | 16 | music | 2004-08-14 |
+-------+--------+------+------+----------+------------+
2 rows in set (0.002 sec)
6,IN
MariaDB [mydb]> select * from students where age in (13,14);
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
+-------+--------+------+------+----------+------------+
2 rows in set (0.001 sec)
7,BETWEEN AND
MariaDB [mydb]> select * from students where age between 15 and 18;
+-------+-----------+------+------+------------+------------+
| id | name | sex | age | interest | birthday |
+-------+-----------+------+------+------------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20003 | 小張 | 男 | 15 | basketball | 2005-08-14 |
| 20005 | 麗麗 | 女 | 15 | painting | 2005-12-01 |
| 20006 | 李明 | 男 | 16 | music | 2004-08-14 |
| 20007 | 張大明 | 男 | 18 | football | 2002-03-05 |
+-------+-----------+------+------+------------+------------+
5 rows in set (0.000 sec)
8,IS NULL
首先確保我的表格中的某些數據是null纔可以使用
update colin set name='Chen' where age=null;
是修改不了數據的因爲age=null 最後的結果是false 條件是不成立的
update colin set name='Chen' where 1=1; 條件成立,但是這種條件沒有任何意義
update colin set name='Chen' where age is null; 正確
9,NOT
MariaDB [mydb]> select * from students where not age=15;
+-------+-----------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+-----------+------+------+----------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
| 20006 | 李明 | 男 | 16 | music | 2004-08-14 |
| 20007 | 張大明 | 男 | 18 | football | 2002-03-05 |
+-------+-----------+------+------+----------+------------+
5 rows in set (0.000 sec)
MariaDB [mydb]> select * from students where age not between 15 and 18;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
+-------+--------+------+------+----------+------------+
2 rows in set (0.001 sec)