当前位置:网站首页>【MySQL】錶數據的增删查改(DML)
【MySQL】錶數據的增删查改(DML)
2022-06-10 22:08:00 【yuelinghou】
文章目錄
一. 插入語句 — insert
語法:
insert into 錶名 (需要插入的字段列錶) values (第一行數據), (第二行數據), ... ,(第n行數據);
舉例:
創建一張Student錶,用來存儲學生的身份信息:
id:代錶學號,設為自增長的主鍵,不允許為空。name:學生姓名,不能為空。qq:學生的qq號,可以為空但不能重複。
mysql> create table if not exists Student(
-> id int unsigned primary key auto_increment,
-> name varchar(20) not null,
-> qq varchar(30) unique
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc Student;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| qq | varchar(30) | YES | UNI | NULL | |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
1. 指定 or 全字段插入
在插入時,我們可以在錶名後括號中指定具體需要插入那個字段;如果不指定就默認是全字段的數據插入:
// 1、指定字段插入一行數據,沒有指定的字段采用默認值
mysql> insert into Student (name, qq) values ('張三', '123456');
Query OK, 1 row affected (0.01 sec)
// 2、全字段插入數據
mysql> insert into Student values (20204912, '李四', '456789');
Query OK, 1 row affected (0.01 sec)
mysql> select * from Student;
+----------+--------+--------+
| id | name | qq |
+----------+--------+--------+
| 1 | 張三 | 123456 |
| 20204912 | 李四 | 456789 |
+----------+--------+--------+
2 rows in set (0.00 sec)
2. 多行數據插入
在插入時,我們可以在values後面進行多行數據的插入,每一個空格內就是一行數據,空格之間用逗號分隔:
// 向學生錶中同時插入兩行數據
mysql> insert into Student (name, qq) values ('朱五', '111111'), ('趙六', '222222');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from Student;
+----------+--------+--------+
| id | name | qq |
+----------+--------+--------+
| 1 | 張三 | 123456 |
| 20204912 | 李四 | 456789 |
| 20204913 | 朱五 | 111111 |
| 20204914 | 趙六 | 222222 |
+----------+--------+--------+
4 rows in set (0.00 sec)
3. key不沖突直接插入,沖突的話就修改
在插入時經常會出現由於主鍵或者唯一鍵對應的值已經存在而導致的插入失敗,插入失敗後我們又要删除該行重新插入。對此,我們在insert插入時配合使用on duplicate key可以完成沒有key沖突時正常插入;有key沖突時進行數據更新的工作,具體語法如下:
insert into ... on duplicate key update 字段1=更新後的值,...,字段n=更新後的值;
下面依然以Stdent錶做演示,注意Student錶中的id字段是主鍵、qq字段是唯一鍵,而name就是一個普通的字段。
0 row affected:錶中有沖突的主鍵或唯一鍵,但沖突數據的值和update的值相等
mysql> select * from Student;
+----------+--------+--------+
| id | name | qq |
+----------+--------+--------+
| 1 | 張三 | 123456 |
| 20204912 | 李四 | 456789 |
| 20204913 | 朱五 | 111111 |
| 20204914 | 趙六 | 222222 |
+----------+--------+--------+
4 rows in set (0.00 sec)
mysql> insert into Student values (1, '張三', '123456')
-> on duplicate key update name='張三';
Query OK, 0 rows affected (0.00 sec)
// 因為沖突數據的值和update的值相等,所以相當於沒有更新
mysql> select * from Student;
+----------+--------+--------+
| id | name | qq |
+----------+--------+--------+
| 1 | 張三 | 123456 |
| 20204912 | 李四 | 456789 |
| 20204913 | 朱五 | 111111 |
| 20204914 | 趙六 | 222222 |
+----------+--------+--------+
4 rows in set (0.00 sec)
1 row affected:錶中沒有沖突的主鍵或唯一鍵,數據被插入
mysql> select * from Student;
+----------+--------+--------+
| id | name | qq |
+----------+--------+--------+
| 1 | 張三 | 123456 |
| 20204912 | 李四 | 456789 |
| 20204913 | 朱五 | 111111 |
| 20204914 | 趙六 | 222222 |
+----------+--------+--------+
4 rows in set (0.00 sec)
mysql> insert into Student values (2, '王二麻子', '333333')
-> on duplicate key update name='王二麻子2';
Query OK, 1 row affected (0.00 sec)
// insert的數據沒有發生主鍵或唯一鍵沖突,作為一行新的數據直接插入
mysql> select * from Student;
+----------+--------------+--------+
| id | name | qq |
+----------+--------------+--------+
| 1 | 張三 | 123456 |
| 2 | 王二麻子 | 333333 |
| 20204912 | 李四 | 456789 |
| 20204913 | 朱五 | 111111 |
| 20204914 | 趙六 | 222222 |
+----------+--------------+--------+
5 rows in set (0.00 sec)
2 row affected:錶中有沖突的主鍵或唯一鍵,並且數據已經被更新
mysql> select * from Student;
+----------+--------------+--------+
| id | name | qq |
+----------+--------------+--------+
| 1 | 張三 | 123456 |
| 2 | 王二麻子 | 333333 |
| 20204912 | 李四 | 456789 |
| 20204913 | 朱五 | 111111 |
| 20204914 | 趙六 | 222222 |
+----------+--------------+--------+
5 rows in set (0.00 sec)
mysql> insert into Student values (1, '張三', '123456')
-> on duplicate key update name='木頭老七', qq='444444';
Query OK, 2 rows affected (0.00 sec)
// 因為主鍵或唯一鍵沖突的原因導致插入失敗
// 但是我們更新了沖突行的字段數據
mysql> select * from Student;
+----------+--------------+--------+
| id | name | qq |
+----------+--------------+--------+
| 1 | 木頭老七 | 444444|
| 2 | 王二麻子 | 333333 |
| 20204912 | 李四 | 456789 |
| 20204913 | 朱五 | 111111 |
| 20204914 | 趙六 | 222222 |
+----------+--------------+--------+
5 rows in set (0.00 sec)
4. 替換 ---- replace
replace相當於insert和duplicate語句結合使用封裝出來的一個新SQL語句,它有如下特點:
1 row affected:主鍵 或者 唯一鍵 沒有沖突,則直接插入。2 row affected:主鍵 或者 唯一鍵 如果沖突,則删除後再插入。
PS:沒有0 row affected的情况了,如果替換數據一模一樣的已經存在則歸於2 row affected的情况。總結起來就是主鍵或唯一鍵沖突就删除重新插入,不沖突的話直接插入。
語法:
replace into 錶名 (字段列錶) values (第一行數據),...,(第n行數據);
舉例:
mysql> select * from Student;
+----------+--------------+--------+
| id | name | qq |
+----------+--------------+--------+
| 1 | 木頭老七 | 444444 |
| 2 | 王二麻子 | 333333 |
| 20204912 | 李四 | 456789 |
| 20204913 | 朱五 | 111111 |
| 20204914 | 趙六 | 222222 |
+----------+--------------+--------+
5 rows in set (0.00 sec)
// 1、替換數據一模一樣的已經存在,删除後重新插入
mysql> replace into Student values (1, '木頭老七', '444444');
Query OK, 2 rows affected (0.01 sec)
// 2、替換數據發生主鍵或唯一鍵沖突,則删除後再插入
mysql> replace into Student values (1, '公孫離', '444444');
Query OK, 2 rows affected (0.01 sec)
// 3、替換數據沒有發生主鍵或唯一鍵沖突,直接插入錶中
mysql> replace into Student values (3, '諸葛亮', '55555');
Query OK, 1 row affected (0.01 sec)
mysql> select * from Student;
+----------+--------------+--------+
| id | name | qq |
+----------+--------------+--------+
| 1 | 公孫離 | 444444 |
| 2 | 王二麻子 | 333333 |
| 3 | 諸葛亮 | 55555 |
| 20204912 | 李四 | 456789 |
| 20204913 | 朱五 | 111111 |
| 20204914 | 趙六 | 222222 |
+----------+--------------+--------+
6 rows in set (0.00 sec)
二. 查詢語句 — select
select語句可以完成錶數據的具體查詢,搭配篩選語句使得select的查詢更加靈活。
為了方便舉例,在這裏新建一張學生成績錶,下面的所有文字內容統一使用這張錶。它包括如下5個字段:
id:學生的學號,設為自增長的主鍵。name:學生姓名,不允許為空。chinese:語文成績,可以為空,默認0.0分。math:數學成績,可以為空,默認0.0分。english:英語成績,可以為空,默認0.0分。
mysql> create table if not exists TestScores(
-> id int unsigned primary key auto_increment,
-> name varchar(20) not null,
-> chinese float default 0.0,
-> math float default 0.0,
-> english float default 0.0
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc TestScores;
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| chinese | float | YES | | 0 | |
| math | float | YES | | 0 | |
| english | float | YES | | 0 | |
+---------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
接下來向該錶中插入數據一些數據:
mysql> insert into TestScores(name, chinese, math, english) values
-> ('曹操', 67, 98, 56),
-> ('孫權', 87, 78, 77),
-> ('孫策', 88, 98, 90),
-> ('劉備', 82, 84, 67),
-> ('程咬金', 55, 85, 45),
-> ('孫尚香', 70, 73, 78),
-> ('諸葛亮', 75, 65, 30);
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
1. 全字段查詢
語法:
select * from 錶名;
說明:
- 這裏 * 號的作用類似於Linux命令的通配符,錶示全部的意思。
- 通常情况下不建議使用 * 進行全列查詢,原因如下:
- 查詢的列越多,意味著需要傳輸的數據量越大。
- 可能會影響到索引的使用。
舉例:
查詢TestCsores錶中所有字段的數據:
mysql> select * from TestScores;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 曹操 | 67 | 98 | 56 |
| 2 | 孫權 | 87 | 78 | 77 |
| 3 | 孫策 | 88 | 98 | 90 |
| 4 | 劉備 | 82 | 84 | 67 |
| 5 | 程咬金 | 55 | 85 | 45 |
| 6 | 孫尚香 | 70 | 73 | 78 |
| 7 | 諸葛亮 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
2. 指定字段查詢
我們也可以查詢錶中特定字段的數據,其中指定字段的順序不需要按定義錶的順序來:
語法:
select後面直接跟需要查詢的字段即可,如果有多個就用逗號分隔且不用加括號。
舉例:
查詢TestScores錶中學生的數學成績:
mysql> select name, math from TestScores;
+-----------+------+
| name | math |
+-----------+------+
| 曹操 | 98 |
| 孫權 | 78 |
| 孫策 | 98 |
| 劉備 | 84 |
| 程咬金 | 85 |
| 孫尚香 | 73 |
| 諸葛亮 | 65 |
+-----------+------+
7 rows in set (0.00 sec)
3. 查詢錶達式結果
select還有計算錶達式的功能:
mysql> select 1+1;
+-----+
| 1+1 |
+-----+
| 2 |
+-----+
1 row in set (0.00 sec)
這個錶達式還可以是錶中的字段組合而成:
mysql> select name, math, chinese, english, math+chinese+english from TestScores;
+-----------+------+---------+---------+----------------------+
| name | math | chinese | english | math+chinese+english |
+-----------+------+---------+---------+----------------------+
| 曹操 | 98 | 67 | 56 | 221 |
| 孫權 | 78 | 87 | 77 | 242 |
| 孫策 | 98 | 88 | 90 | 276 |
| 劉備 | 84 | 82 | 67 | 233 |
| 程咬金 | 85 | 55 | 45 | 185 |
| 孫尚香 | 73 | 70 | 78 | 221 |
| 諸葛亮 | 65 | 75 | 30 | 170 |
+-----------+------+---------+---------+----------------------+
7 rows in set (0.00 sec)
在錶達式後面加上as還可以對錶達式重命名:
mysql> select name, math, chinese, english, math+chinese+english as total from TestScores;
+-----------+------+---------+---------+-------+
| name | math | chinese | english | total |
+-----------+------+---------+---------+-------+
| 曹操 | 98 | 67 | 56 | 221 |
| 孫權 | 78 | 87 | 77 | 242 |
| 孫策 | 98 | 88 | 90 | 276 |
| 劉備 | 84 | 82 | 67 | 233 |
| 程咬金 | 85 | 55 | 45 | 185 |
| 孫尚香 | 73 | 70 | 78 | 221 |
| 諸葛亮 | 65 | 75 | 30 | 170 |
+-----------+------+---------+---------+-------+
7 rows in set (0.00 sec)
4. 查詢結果去重
在錶中,可能會包含重複值。這並不成問題,不過,有時您也許希望僅僅列出不同(distinct)的值,distinct關鍵字可以幫助我們完成去重工作。
語法:
select distinct 字段名稱 from錶名稱;
舉例:
// 1、查詢錶中學生的數學成績,發現98分重複了
mysql> select math from TestScores;
+------+
| math |
+------+
| 98 |
| 78 |
| 98 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
7 rows in set (0.00 sec)
// 2、使用distinct去重
mysql> select distinct math from TestScores;
+------+
| math |
+------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
6 rows in set (0.00 sec)
三. 篩選字句 — where
1. 介紹
where子句用於規定選擇的標准,通常需要搭配其他語句使用。下面的運算符可在 where子句中使用:
| 運算符 | 說明 |
|---|---|
| (>, >=, <, <=) | (大於、大於等於、小於、小於等於) |
| = | 等於,NULL 不安全,例如 NULL = NULL 的結果是 NULL |
| <=> | 等於,NULL 安全,例如 NULL <=> NULL 的結果是 TRUE(1) |
| !=, <> | 不等於 |
| BETWEEN a0 AND a1 | 範圍匹配:[a0, a1] 如果 a0 <= value <= a1,返回 TRUE(1) |
| IN (option, …) | 如果是 option 中的任意一個,返回 TRUE(1) |
| IS NULL | 是 NULL |
| IS NOT NULL | 不是 NULL |
| LIKE | 模糊匹配。% 錶示任意多個(包括 0 個)任意字符;_ 錶示任意一個字符 |
| AND | 多個條件必須都為 TRUE(1),結果才是 TRUE(1) |
| OR | 任意一個條件為 TRUE(1), 結果為 TRUE(1) |
| NOT | 條件為 TRUE(1),結果為 FALSE(0) |
注意:在寫SQL時,語(子)句之間用空格隔開,字段之間用逗號隔開。
2. 舉例
1、查詢英語不及格的同學及他的英語成績 (<60)
mysql> select name, english from TestScores where english<60;
+-----------+---------+
| name | english |
+-----------+---------+
| 曹操 | 56 |
| 程咬金 | 45 |
| 諸葛亮 | 30 |
+-----------+---------+
3 rows in set (0.00 sec)
2、語文成績在 [80, 90] 分的同學及他的語文成績
// 方法一:使用 AND 進行條件連接
mysql> select name, chinese from TestScores where chinese>=80 and chinese<=90;
+--------+---------+
| name | chinese |
+--------+---------+
| 孫權 | 87 |
| 孫策 | 88 |
| 劉備 | 82 |
+--------+---------+
3 rows in set (0.00 sec)
// 方法二:使用 BETWEEN ... AND ... 條件進行連接
mysql> select name, chinese from TestScores where chinese between 80 and 90;
+--------+---------+
| name | chinese |
+--------+---------+
| 孫權 | 87 |
| 孫策 | 88 |
| 劉備 | 82 |
+--------+---------+
3 rows in set (0.00 sec)
3、數學成績是 58 或者 59 或者 98 或者 99 分的同學及數學成績
// 方法一:使用 OR 進行條件連接
mysql> select name, math from TestScores where math=58 or math=59 or math=98 or math=99;
+--------+------+
| name | math |
+--------+------+
| 曹操 | 98 |
| 孫策 | 98 |
+--------+------+
2 rows in set (0.00 sec)
// 方法二:使用 IN 進行條件篩選
mysql> select name, math from TestScores where math in (58, 59, 98, 99);
+--------+------+
| name | math |
+--------+------+
| 曹操 | 98 |
| 孫策 | 98 |
+--------+------+
2 rows in set (0.00 sec)
4、查詢姓孫的同學 及 孫某同學
// %匹配任意多個(包括 0 個)字符
mysql> select name from TestScores where name like '孫%';
+-----------+
| name |
+-----------+
| 孫權 |
| 孫策 |
| 孫尚香 |
+-----------+
3 rows in set (0.00 sec)
// _嚴格匹配的一個任意字符
mysql> select name from TestScores where name like '孫_';
+--------+
| name |
+--------+
| 孫權 |
| 孫策 |
+--------+
2 rows in set (0.00 sec)
5、語文成績好於英語成績的同學
mysql> select name, chinese, english from TestScores where chinese > english;
+-----------+---------+---------+
| name | chinese | english |
+-----------+---------+---------+
| 曹操 | 67 | 56 |
| 孫權 | 87 | 77 |
| 劉備 | 82 | 67 |
| 程咬金 | 55 | 45 |
| 諸葛亮 | 75 | 30 |
+-----------+---------+---------+
5 rows in set (0.00 sec)
6、查詢語文成績比英語成績好30分以上的同學
mysql> select name, chinese, english from TestScores where chinese > english+30;
+-----------+---------+---------+
| name | chinese | english |
+-----------+---------+---------+
| 諸葛亮 | 75 | 30 |
+-----------+---------+---------+
1 row in set (0.00 sec)
7、總分在 200 分以下的同學
注意where字句中只能識別錶的字段,但不能識別錶達式的別名,因為where字句是和select查詢同步進行的,查詢完成之後才會進行錶達式的計算。
// error
mysql> select name, chinese+math+english as total from TestScores where total < 200;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
// succeed
mysql> select name, chinese+math+english as total from TestScores where chinese+math+english < 200;
+-----------+-------+
| name | total |
+-----------+-------+
| 程咬金 | 185 |
| 諸葛亮 | 170 |
+-----------+-------+
2 rows in set (0.00 sec)
8、語文成績 > 80 並且不姓孫的同學
AND與NOT一起使用即可:
mysql> select name, chinese from TestScores where chinese>80 and not name like '孫%';
+--------+---------+
| name | chinese |
+--------+---------+
| 劉備 | 82 |
+--------+---------+
1 row in set (0.00 sec)
9、孫某同學,否則要求總成績 > 200 並且 語文成績 < 數學成績 並且 英語成績 > 80
mysql> select name, chinese, math, english, chinese+math+english as total from TestScores
-> where name like '孫%'
-> or (chinese+math+english > 200 and chinese < math and english > 80);
+-----------+---------+------+---------+-------+
| name | chinese | math | english | total |
+-----------+---------+------+---------+-------+
| 孫權 | 87 | 78 | 77 | 242 |
| 孫策 | 88 | 98 | 90 | 276 |
| 孫尚香 | 70 | 73 | 78 | 221 |
+-----------+---------+------+---------+-------+
3 rows in set (0.00 sec)
10、NULL 的查詢
NULL其他數據比較時:
- =下的NULL是不安全的,比較結果全是NULL。
- <=>下的NULL是安全的,比較結果符合邏輯。

對於!=和<>而言,NULL都是不安全的。所以涉及到NULL的比較時,盡量用<=>,如果要比較不等於的話,在<=>前面加個not即可:
四. 排序字句 — order by
1. 介紹
ORDER BY 語句用於根據指定的列對結果集進行排序,且該語句默認按照昇序對記錄進行排序,當然我們也可以顯示指定排序的方式:
asc:即ascending,為昇序(從小到大)。desc:即descending,為降序(從大到小)。
PS:在MySQL中,NULL視為比任何值都小。
2. 舉例
1、查詢同學及數學成績,按數學成績昇序顯示
// 1、未排序的結果
mysql> select name, math from TestScores;
+-----------+------+
| name | math |
+-----------+------+
| 曹操 | 98 |
| 孫權 | 78 |
| 孫策 | 98 |
| 劉備 | 84 |
| 程咬金 | 85 |
| 孫尚香 | 73 |
| 諸葛亮 | 65 |
+-----------+------+
7 rows in set (0.00 sec)
// 2、order by字句默認就是昇序
mysql> select name, math from TestScores order by math;
+-----------+------+
| name | math |
+-----------+------+
| 諸葛亮 | 65 |
| 孫尚香 | 73 |
| 孫權 | 78 |
| 劉備 | 84 |
| 程咬金 | 85 |
| 曹操 | 98 |
| 孫策 | 98 |
+-----------+------+
7 rows in set (0.00 sec)
// 3、當然也可以手動加上asc昇序排序
mysql> select name, math from TestScores order by math asc;
+-----------+------+
| name | math |
+-----------+------+
| 諸葛亮 | 65 |
| 孫尚香 | 73 |
| 孫權 | 78 |
| 劉備 | 84 |
| 程咬金 | 85 |
| 曹操 | 98 |
| 孫策 | 98 |
+-----------+------+
7 rows in set (0.00 sec)
2、查詢同學各門成績,依次按 數學降序,英語昇序,語文昇序的方式顯示
多字段排序時,排序優先級隨order by後面的書寫順序而定:
mysql> select name, math, english, chinese from TestScores
-> order by math desc, english, chinese;
+-----------+------+---------+---------+
| name | math | english | chinese |
+-----------+------+---------+---------+
| 曹操 | 98 | 56 | 67 |
| 孫策 | 98 | 90 | 88 |
| 程咬金 | 85 | 45 | 55 |
| 劉備 | 84 | 67 | 82 |
| 孫權 | 78 | 77 | 87 |
| 孫尚香 | 73 | 78 | 70 |
| 諸葛亮 | 65 | 30 | 75 |
+-----------+------+---------+---------+
7 rows in set (0.00 sec)
3、查詢同學及總分,由高到低
與where字句不同的是,order by子句中可以使用錶達式或字段的別名,因為它們組合使用時的執行邏輯如下:
- 先根據where字句給出的條件篩選出原錶格中滿足條件的數據。
- 依據這些篩選出來的數據完成錶達式的計算和一些重命名工作。
- 然後執行order by字句對上面的數據進行排序。
- 最後執行limit分頁(這個後面講解)。
// ORDER BY 子句中可以使用列別名
mysql> select name, chinese+math+english as total from TestScores
-> order by total desc;
+-----------+-------+
| name | total |
+-----------+-------+
| 孫策 | 276 |
| 孫權 | 242 |
| 劉備 | 233 |
| 曹操 | 221 |
| 孫尚香 | 221 |
| 程咬金 | 185 |
| 諸葛亮 | 170 |
+-----------+-------+
7 rows in set (0.00 sec)
4、查詢姓孫的同學或者姓曹的同學數學成績,結果按數學成績由高到低顯示
結合 where子句和order by子句,注意它們的執行順序:要先篩選然後才能排序:
mysql> select name, math from TestScores
-> where name like '孫%' or name like '曹%'
-> order by math desc;
+-----------+------+
| name | math |
+-----------+------+
| 曹操 | 98 |
| 孫策 | 98 |
| 孫權 | 78 |
| 孫尚香 | 73 |
+-----------+------+
4 rows in set (0.00 sec)
五. 分頁字句 — limit
1. 配合查詢時的介紹
- 從0下標開始,查詢n條結果:
SELECT ... FROM 錶名 [WHERE ...] [ORDER BY ...] LIMIT n;
- 從s開始,篩選n條結果(用法類似於std::string::substr(…)):
SELECT ... FROM 錶名 [WHERE ...] [ORDER BY ...] LIMIT s, n;
- 從s開始,篩選n條結果,比第二種用法更明確,建議使用(其中offset是偏移量的意思):
SELECT ... FROM 錶名 [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
使用建議:對未知錶進行查詢時,最好加一條limit 1來先觀察錶的基本數據和結構;避免因為錶中數據過多,查詢全錶數據導致數據庫卡死。
2. 舉例
按 id 進行分頁,每頁 3 條記錄,然後分別顯示 第 1、2、3 頁:
// 第1頁
mysql> select name, chinese, math, english from TestScores limit 0, 3;
+--------+---------+------+---------+
| name | chinese | math | english |
+--------+---------+------+---------+
| 曹操 | 67 | 98 | 56 |
| 孫權 | 87 | 78 | 77 |
| 孫策 | 88 | 98 | 90 |
+--------+---------+------+---------+
3 rows in set (0.00 sec)
// 第2頁
mysql> select name, math, english from TestScores limit 3, 3;
+-----------+------+---------+
| name | math | english |
+-----------+------+---------+
| 劉備 | 84 | 67 |
| 程咬金 | 85 | 45 |
| 孫尚香 | 73 | 78 |
+-----------+------+---------+
3 rows in set (0.00 sec)
// 第3頁,如果結果不足3個,不會有影響
mysql> select name, math, english from TestScores limit 6, 3;
+-----------+------+---------+
| name | math | english |
+-----------+------+---------+
| 諸葛亮 | 65 | 30 |
+-----------+------+---------+
1 row in set (0.01 sec)
PS:這種分頁且每頁固定有n條數據的場景還是很常見的,比如我們在搜索引擎上搜索一個關鍵字時,整個頁面就是一種分頁的結果顯示出來的:
六. 修改語句 — update
1. 介紹
update使用時通常需要結合where字句去篩選出特定的某一行數據然後再對其中的某個字段的值做修改;如果不篩選的話,默認修改的的是整個錶格的數據。
語法:
update 錶名稱 set 列名稱=新值 where 列名稱=某值;
PS:為了更靈活篩選,update還可以配合where、order by和limit一起使用,具體看下面的例子。
2. 舉例
1、將曹操同學的語文成績變更為 80 分
//1、查看修改前曹操同學的語文成績
mysql> select name, chinese from TestScores where name='曹操';
+--------+---------+
| name | chinese |
+--------+---------+
| 曹操 | 67 |
+--------+---------+
1 row in set (0.00 sec)
// 2、修改曹操同學的語文成績為80分
mysql> update TestScores set chinese=80 where name='曹操';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
// 3、查看修改後曹操同學的語文成績
mysql> select name, chinese from TestScores where name='曹操';
+--------+---------+
| name | chinese |
+--------+---------+
| 曹操 | 80 |
+--------+---------+
1 row in set (0.00 sec)
2、將曹操同學的數學成績變更為 60 分,語文成績變更為 70 分
// 1、修改前
mysql> select name, math, chinese from TestScores where name='曹操';
+--------+------+---------+
| name | math | chinese |
+--------+------+---------+
| 曹操 | 98 | 80 |
+--------+------+---------+
1 row in set (0.00 sec)
// 2、修改ing
mysql> update TestScores set math=60, chinese=70 where name='曹操';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
// 3、修改後
mysql> select name, math, chinese from TestScores where name='曹操';
+--------+------+---------+
| name | math | chinese |
+--------+------+---------+
| 曹操 | 60 | 70 |
+--------+------+---------+
1 row in set (0.00 sec)
3、將總成績倒數前三的 3 比特同學的數學成績加上 30 分
PS:數據更新,不支持 math += 30 這種語法,只能math = math + 30。
// 1、查看原數據
mysql> select name, math from TestScores order by chinese+math+english limit 3;
+-----------+------+
| name | math |
+-----------+------+
| 諸葛亮 | 65 |
| 程咬金 | 85 |
| 曹操 | 60 |
+-----------+------+
3 rows in set (0.00 sec)
// 2、修改數據
mysql> update TestScores set math=math+30 order by chinese+math+english limit 3;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0
// 3、查看修改後的數據
mysql> select name, math from TestScores where name in ('諸葛亮', '程咬金', '曹操');
+-----------+------+
| name | math |
+-----------+------+
| 曹操 | 90 |
| 程咬金 | 115 |
| 諸葛亮 | 95 |
+-----------+------+
3 rows in set (0.00 sec)
七. 删除語句 — delete
1. 介紹
delete語句用於删除錶中的行數據。
具體要删除那一行,可以通過where、order by、limit等字句篩選出來,語法如下:
delete from 錶名稱 where 列名稱=值;
2. 舉例
1、删除曹操同學的考試成績
// 1、查看原數據
mysql> select name from TestScores where name='曹操';
+--------+
| name |
+--------+
| 曹操 |
+--------+
1 row in set (0.00 sec)
// 2、删除name='曹操'那行的所有數據
mysql> delete from TestScores where name='曹操';
Query OK, 1 row affected (0.01 sec)
// 3、查看删除結果
mysql> select name from TestScores where name='曹操';
Empty set (0.00 sec)
3. 清空錶數據
為了測試清空錶的操作,我們成建一張測試錶ForDelete用來舉例:
mysql> create table if not exists ForDelete(
-> id int unsigned primary key auto_increment,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc ForDelete;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
接下來向錶中,插入三行數據:
mysql> insert into ForDelete (name) values ('a'), ('b'), ('c');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from ForDelete;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
+----+------+
3 rows in set (0.01 sec)
接下來介紹兩種清空錶數據的方法。
方法一:delete from 錶名
// 1、使用delete語句來清空錶數據
mysql> delete from ForDelete;
Query OK, 3 rows affected (0.00 sec)
// 2、向錶中重新插入三條數據
mysql> insert into ForDelete (name) values ('d'), ('e'), ('f');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
// 3、觀察新插入的數據情况
mysql> select * from ForDelete;
+----+------+
| id | name |
+----+------+
| 4 | d |
| 5 | e |
| 6 | f |
+----+------+
3 rows in set (0.00 sec)
使用delete情况錶數據之後,發現重新插入的數據它的自增長值依然是在原來基礎上遞增的,說明delete清空錶並沒有清除自增長的記錄值。我們查看該錶的創建語句,發現確實還記錄著auto_increment的值:
方法二:truncate table 錶名
truncate 有截斷的意思,該語句有如下三點需要注意:
- 只能對整錶操作,不能像 DELETE 一樣針對部分數據操作。
- 截斷時 MySQL 不對數據操作,所以比 DELETE 更快,但是TRUNCATE在删除數據的時候,並不經過真正的事物,所以無法回滾。
- 該操作會重置錶的 AUTO_INCREMENT 項。
舉例:還是上面的測試錶ForDelete,這次我們使用truncate來清空錶的數據。
// 1、清空之前錶中的數據
mysql> select * from ForDelete;
+----+------+
| id | name |
+----+------+
| 4 | d |
| 5 | e |
| 6 | f |
+----+------+
3 rows in set (0.00 sec)
// 2、使用truncate清空錶中的數據
mysql> truncate table ForDelete;
Query OK, 0 rows affected (0.05 sec)
// 3、清空完成
mysql> select * from ForDelete;
Empty set (0.00 sec)
還是一樣的,向這個已經被清空的錶中重新插入三組數據,觀察自增長值是怎麼分配的:
mysql> insert into ForDelete (name) values ('g'), ('h'), ('i');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from ForDelete;
+----+------+
| id | name |
+----+------+
| 1 | g |
| 2 | h |
| 3 | i |
+----+------+
3 rows in set (0.00 sec)
觀察新插入的三行數據,發現原來錶的自增長值被重置了。最後我們還可以來看看該錶的創建語句是否還記錄著auto_increment的值:
八. DML使用總結
- 語(子)句之間用空格隔開,字段和()之間用逗號隔開。
- 查詢時的執行順序:where子句 + 其他語句 —> 錶達式計算+重命名 —> order by字句 —> limit字句。
- where字句中不能識別重命名,其他字句可以。
边栏推荐
- protoc protoc-gen-go protobuf 之间的关系
- sql server行转列(pivot),列转行(unpivot)
- MySQL数据库如何查看表占用空间大小
- In 2021, the average salary will be released, and the IT industry will not be surprised
- System reinstallation and system performance query
- 旋转菜单2.0
- 2021年平均工资出炉,IT行业不出所料
- How to choose the field of we media video creation?
- An analysis of SQL query optimization principle (900w+ data from 17s to 300ms)
- [nk] 牛客月賽51 G計算題
猜你喜欢

What do software test engineers do?

【北大青鸟昌平校区】职教与普教协调发展,今年的中考会容易吗?

Course design of imitation pottery ticket of wechat applet

NFT copyright / royalties

Acl2022 | bert2bert: an efficient pre training method of parameter reuse, which significantly reduces the training cost of oversized models

Redis cache penetration

php的exec函数

入行须知:运维需要懂编程吗?

C language ---9 first knowledge of macros and pointers

【MySQL】表数据的增删查改(DML)
随机推荐
磁盘序列号,磁盘ID,卷序列号的区别
详解MATLAB中与矩阵运算有关的算术运算符(加、减、乘、除、点乘、点除、乘方)
Introduction to ZigBee module wireless transmission star topology networking structure
【MySQL】表的约束
C language ---9 first knowledge of macros and pointers
01js基础 null与undefined区别 类型转换 == 代码块 逻辑运算符
Factory and strategy mode implementation scheme of coupons
Rotate menu 2.0
php伪协议实现命令执行详情
Rotate menu 3.0
2022-06-09 RK817 PMU 電池溫度檢測
C language ---6 first knowledge of selection statement, loop statement, function and array
Explain in detail the arithmetic operators related to matrix operation in MATLAB (addition, subtraction, multiplication, division, point multiplication, point division, power)
系统重装以及查询系统性能
Notes to entry: do I need to know programming for O & M?
MySQL范围查询优化的场景实例详解
JS anchor positioning can extend many functions
C language ---2 initial knowledge of data types
In 2021, the average salary will be released, and the IT industry will not be surprised
C language -- 7 operators