前言
MySQL的 CURD 是一個(gè)數(shù)據(jù)庫(kù)技術(shù)中的縮寫(xiě)詞,一般的項(xiàng)目開(kāi)發(fā)的各種參數(shù)的基本功能都是CURD,他的作用是用于處理數(shù)據(jù)的基本原子操作。CURD 分別代表創(chuàng)建(Create)、更新(Update)、讀?。≧ead)和刪除(Delete)操作。
一、Create操作
新增數(shù)據(jù)的語(yǔ)法:
INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...
value_list: value, [, value] ...
案例:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-432904.html
-- 創(chuàng)建一張學(xué)生表
CREATE TABLE students(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
sn INT NOT NULL UNIQUE COMMENT '學(xué)號(hào)',
name VARCHAR(20) NOT NULL,
qq VARCHAR(20)
);
1.1 單行數(shù)據(jù)全列插入
插入兩條記錄,value_list 數(shù)量必須和定義表的列的數(shù)量及順序一致。
注意,這里在插入的時(shí)候,也可以不用指定id (當(dāng)然,那時(shí)候就需要明確插入數(shù)據(jù)到那些列了),那么mysql會(huì)使用默認(rèn)的值進(jìn)行自增。
mysql> insert into students values (100, 10000, '唐三藏', null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into students values (101, 10001, '孫悟空', '11111');
Query OK, 1 row affected (0.00 sec)
-- 查看插入結(jié)果
mysql> select * from students;
+-----+-------+-----------+-------+
| id | sn | name | qq |
+-----+-------+-----------+-------+
| 100 | 10000 | 唐三藏 | NULL |
| 101 | 10001 | 孫悟空 | 11111 |
+-----+-------+-----------+-------+
2 rows in set (0.00 sec)
mysql>
1.2 多行數(shù)據(jù)指定列插入
插入兩條記錄,value_list 數(shù)量必須和指定列數(shù)量及順序一致:
mysql> insert into students (id, sn, name) values
-> (102, 20001, '曹孟德'),
-> (103, 20002, '孫仲謀');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
-- 查看插入結(jié)果
mysql> select * from students;
+-----+-------+-----------+-------+
| id | sn | name | qq |
+-----+-------+-----------+-------+
| 100 | 10000 | 唐三藏 | NULL |
| 101 | 10001 | 孫悟空 | 11111 |
| 102 | 20001 | 曹孟德 | NULL |
| 103 | 20002 | 孫仲謀 | NULL |
+-----+-------+-----------+-------+
4 rows in set (0.01 sec)
mysql>
1.3 插入更新
由于 主鍵 或者 唯一鍵 對(duì)應(yīng)的值已經(jīng)存在而導(dǎo)致插入失敗:
-- 主鍵沖突
mysql> insert into students (id, sn, name) values (100, 10010, '唐玄奘');
ERROR 1062 (23000): Duplicate entry '100' for key 'PRIMARY'
-- 唯一鍵沖突
mysql> insert into students (sn, name) values (20001, '曹阿瞞');
ERROR 1062 (23000): Duplicate entry '20001' for key 'sn'
mysql>
可以選擇性的進(jìn)行 同步更新操作,語(yǔ)法:
INSERT ... ON DUPLICATE KEY UPDATE
column = value [, column = value] ...
例如:
mysql> insert into students (id, sn, name) values (100, 10010, '.玄奘 ')
on duplicate key update sn = 10010, name='.玄奘 ';
Query OK, 2 rows affected (0.00 sec)
mysql> select * from students;
+-----+-------+-----------+-------+
| id | sn | name | qq |
+-----+-------+-----------+-------+
| 100 | 10010 | 唐玄奘 | NULL |
| 101 | 10001 | 孫悟空 | 11111 |
| 102 | 20001 | 曹孟德 | NULL |
| 103 | 20002 | 孫仲謀 | NULL |
+-----+-------+-----------+-------+
4 rows in set (0.00 sec)
其中:
-- 0 row affected: 表中有沖突數(shù)據(jù),但沖突數(shù)據(jù)的值和 update 的值相等
-- 1 row affected: 表中沒(méi)有沖突數(shù)據(jù),數(shù)據(jù)被插入
-- 2 row affected: 表中有沖突數(shù)據(jù),并且數(shù)據(jù)已經(jīng)被更新
-- 通過(guò) MySQL 函數(shù)獲取受到影響的數(shù)據(jù)行數(shù)
SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)
1.4 插入替換
replace
用于替換,其原則是:
- 主鍵 或者 唯一鍵 沒(méi)有沖突,則直接插入;
- 主鍵 或者 唯一鍵 如果沖突,則刪除后再插入
例如:
mysql> replace into students (sn, name) values (20001, '曹阿瞞');
Query OK, 2 rows affected (0.00 sec)
mysql> select * from students;
+-----+-------+-----------+-------+
| id | sn | name | qq |
+-----+-------+-----------+-------+
| 100 | 10010 | 唐玄奘 | NULL |
| 101 | 10001 | 孫悟空 | 11111 |
| 103 | 20002 | 孫仲謀 | NULL |
| 106 | 20001 | 曹阿瞞 | NULL |
+-----+-------+-----------+-------+
4 rows in set (0.00 sec)
mysql>
其中:
-- 1 row affected: 表中沒(méi)有沖突數(shù)據(jù),數(shù)據(jù)被插入
-- 2 row affected: 表中有沖突數(shù)據(jù),刪除后重新插入
二、Read操作
語(yǔ)法:
SELECT
[DISTINCT] {* | {column [, column] ...}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT ...
案例:
-- 創(chuàng)建表結(jié)構(gòu)
CREATE TABLE exam_result (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL COMMENT '同學(xué)姓名',
chinese float DEFAULT 0.0 COMMENT '語(yǔ)文成績(jī)',
math float DEFAULT 0.0 COMMENT '數(shù)學(xué)成績(jī)',
english float DEFAULT 0.0 COMMENT '英語(yǔ)成績(jī)'
);
-- 插入測(cè)試數(shù)據(jù)
INSERT INTO exam_result (name, chinese, math, english) VALUES
('唐三藏', 67, 98, 56),
('孫悟空', 87, 78, 77),
('豬悟能', 88, 98, 90),
('曹孟德', 82, 84, 67),
('劉玄德', 55, 85, 45),
('孫權(quán)', 70, 73, 78),
('宋公明', 75, 65, 30);
2.1 SELECT 操作
2.1.1 全列查詢(xún)
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| 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 | 孫權(quán) | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
【注意】
通常情況下不建議使用*
進(jìn)行全列查詢(xún),原因在于:
- 查詢(xún)的列越多,意味著需要傳輸?shù)臄?shù)據(jù)量越大;
- 可能會(huì)影響到索引的使用。
2.1.2 指定列查詢(xún)
指定列的順序不需要按定義表的順序來(lái):
mysql> select id,name,english from exam_result;
+----+-----------+---------+
| id | name | english |
+----+-----------+---------+
| 1 | 唐三藏 | 56 |
| 2 | 孫悟空 | 77 |
| 3 | 豬悟能 | 90 |
| 4 | 曹孟德 | 67 |
| 5 | 劉玄德 | 45 |
| 6 | 孫權(quán) | 78 |
| 7 | 宋公明 | 30 |
+----+-----------+---------+
7 rows in set (0.00 sec)
mysql>
2.1.3 查詢(xún)字段為表達(dá)式
表達(dá)式不包含字段:
mysql> select id,name, 10 from exam_result;
+----+-----------+----+
| id | name | 10 |
+----+-----------+----+
| 1 | 唐三藏 | 10 |
| 2 | 孫悟空 | 10 |
| 3 | 豬悟能 | 10 |
| 4 | 曹孟德 | 10 |
| 5 | 劉玄德 | 10 |
| 6 | 孫權(quán) | 10 |
| 7 | 宋公明 | 10 |
+----+-----------+----+
7 rows in set (0.00 sec)
表達(dá)式包含一個(gè)字段:
mysql> select id,name, english + 10 from exam_result;
+----+-----------+--------------+
| id | name | english + 10 |
+----+-----------+--------------+
| 1 | 唐三藏 | 66 |
| 2 | 孫悟空 | 87 |
| 3 | 豬悟能 | 100 |
| 4 | 曹孟德 | 77 |
| 5 | 劉玄德 | 55 |
| 6 | 孫權(quán) | 88 |
| 7 | 宋公明 | 40 |
+----+-----------+--------------+
7 rows in set (0.00 sec)
mysql>
表達(dá)式包含多個(gè)字段:
mysql> select id,name, chinese + math + english from exam_result;
+----+-----------+--------------------------+
| id | name | chinese + math + english |
+----+-----------+--------------------------+
| 1 | 唐三藏 | 221 |
| 2 | 孫悟空 | 242 |
| 3 | 豬悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 劉玄德 | 185 |
| 6 | 孫權(quán) | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+--------------------------+
7 rows in set (0.00 sec)
mysql>
2.1.4 為查詢(xún)結(jié)果指定別名
語(yǔ)法:
SELECT column [AS] alias_name […] FROM table_name; - - 其中
AS
可以省略
例如:
mysql> select id,name, chinese + math + english as 總分 from exam_result;
+----+-----------+--------+
| id | name | 總分 |
+----+-----------+--------+
| 1 | 唐三藏 | 221 |
| 2 | 孫悟空 | 242 |
| 3 | 豬悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 劉玄德 | 185 |
| 6 | 孫權(quán) | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+--------+
7 rows in set (0.00 sec)
mysql>
2.1.5 結(jié)果去重
例如:數(shù)學(xué)成績(jī) 98 分重復(fù)了
mysql> select math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 98 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
7 rows in set (0.00 sec)
使用 distinct
進(jìn)行去重:
mysql> select distinct math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
6 rows in set (0.00 sec)
mysql>
2.2 WHERE 條件
2.2.1 比較與邏輯運(yùn)算符
比較運(yùn)算符:
運(yùn)算符 | 說(shuō)明 |
---|---|
>, >=, <, <= | 大于,大于等于,小于,小于等于 |
= | 等于,NULL 不安全,例如 NULL = NULL 的結(jié)果是 NULL |
<=> | 等于,NULL 安全,例如 NULL <=> NULL 的結(jié)果是 TRUE (1) |
!=, <> | 不等于 |
BETWEEN a0 AND a1 | 范圍匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1) |
IN (option, …) | 如果是 option 中的任意一個(gè),返回 TRUE(1) |
IS NULL | 是 NULL |
IS NOT NULL | 不是 NULL |
LIKE | 模糊匹配。% 表示任意多個(gè)(包括 0 個(gè))任意字符;_ 表示任意一個(gè)字符 |
邏輯運(yùn)算符:
運(yùn)算符 | 說(shuō)明 |
---|---|
AND | 多個(gè)條件必須都為 TRUE(1),結(jié)果才是 TRUE(1) |
OR | 任意一個(gè)條件為 TRUE(1), 結(jié)果為 TRUE(1) |
NOT | 條件為 TRUE(1),結(jié)果為 FALSE(0) |
2.2.2 查詢(xún)案例
- 英語(yǔ)不及格的同學(xué)及英語(yǔ)成績(jī) ( < 60 )
mysql> select name, english from exam_result where english < 60;
+-----------+---------+
| name | english |
+-----------+---------+
| 唐三藏 | 56 |
| 劉玄德 | 45 |
| 宋公明 | 30 |
+-----------+---------+
3 rows in set (0.00 sec)
mysql>
- 語(yǔ)文成績(jī)?cè)?[80, 90] 分的同學(xué)及語(yǔ)文成績(jī)
-- 使用 AND 進(jìn)行條件連接
mysql> select name, chinese from exam_result where chinese >= 80 and chinese <= 90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 孫悟空 | 87 |
| 豬悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
3 rows in set (0.00 sec)
mysql>
-- 使用 BETWEEN ... AND ... 條件
mysql> select name, chinese from exam_result where chinese between 80 and 90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 孫悟空 | 87 |
| 豬悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
3 rows in set (0.00 sec)
mysql>
- 數(shù)學(xué)成績(jī)是 58 或者 59 或者 98 或者 99 分的同學(xué)及數(shù)學(xué)成績(jī)
-- 使用 OR 進(jìn)行條件連接
mysql> select name, math from exam_result where math = 58 or math = 59 or math = 98 or math = 99;
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 豬悟能 | 98 |
+-----------+------+
2 rows in set (0.01 sec)
-- 使用 IN 條件
mysql> select name, math from exam_result where math in (58, 59, 98, 99);
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 豬悟能 | 98 |
+-----------+------+
2 rows in set (0.00 sec)
mysql>
- 姓孫的同學(xué) 及 孫某同學(xué)
-- % 匹配任意多個(gè)(包括 0 個(gè))任意字符
mysql> select name from exam_result where name like '孫%';
+-----------+
| name |
+-----------+
| 孫悟空 |
| 孫權(quán) |
+-----------+
2 rows in set (0.00 sec)
-- _ 匹配嚴(yán)格的一個(gè)任意字符
mysql> select name from exam_result where name like '孫_';
+--------+
| name |
+--------+
| 孫權(quán) |
+--------+
1 row in set (0.00 sec)
mysql>
- 語(yǔ)文成績(jī)好于英語(yǔ)成績(jī)的同學(xué)
-- WHERE 條件中比較運(yùn)算符兩側(cè)都是字段
mysql> select name, chinese, english from exam_result where chinese > english;
+-----------+---------+---------+
| name | chinese | english |
+-----------+---------+---------+
| 唐三藏 | 67 | 56 |
| 孫悟空 | 87 | 77 |
| 曹孟德 | 82 | 67 |
| 劉玄德 | 55 | 45 |
| 宋公明 | 75 | 30 |
+-----------+---------+---------+
5 rows in set (0.00 sec)
mysql>
- 總分在 200 分以下的同學(xué)
-- WHERE 條件中使用表達(dá)式
mysql> SELECT name, chinese + math + english 總分 FROM exam_result WHERE chinese + math + english < 200;
+-----------+--------+
| name | 總分 |
+-----------+--------+
| 劉玄德 | 185 |
| 宋公明 | 170 |
+-----------+--------+
2 rows in set (0.01 sec)
mysql>
注意:
- 別名不能用在 WHERE 條件中
- 語(yǔ)文成績(jī) > 80 并且不姓孫的同學(xué)
-- AND 與 NOT 的使用
mysql> select name, chinese from exam_result where chinese > 80 and name not like '孫%';
+-----------+---------+
| name | chinese |
+-----------+---------+
| 豬悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
2 rows in set (0.01 sec)
mysql>
- 孫某同學(xué),否則要求總成績(jī) > 200 并且 語(yǔ)文成績(jī) < 數(shù)學(xué)成績(jī) 并且 英語(yǔ)成績(jī) > 80
-- 綜合性查詢(xún)
mysql> SELECT name, chinese, math, english, chinese + math + english 總分
-> FROM exam_result
-> WHERE name LIKE '孫_' OR (
-> chinese + math + english > 200 AND chinese < math AND english > 80
-> );
+-----------+---------+------+---------+--------+
| name | chinese | math | english | 總分 |
+-----------+---------+------+---------+--------+
| 豬悟能 | 88 | 98 | 90 | 276 |
| 孫權(quán) | 70 | 73 | 78 | 221 |
+-----------+---------+------+---------+--------+
2 rows in set (0.00 sec)
2.3 ORDER BY結(jié)果排序
2.3.1 排序語(yǔ)法
-- ASC 為升序(從小到大)
-- DESC 為降序(從大到?。?/span>
-- 默認(rèn)為 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
注意:沒(méi)有 ORDER BY 子句的查詢(xún),返回的順序是未定義的,永遠(yuǎn)不要依賴(lài)這個(gè)順序。
2.3.2 排序案例
- 同學(xué)名及數(shù)學(xué)成績(jī),按數(shù)學(xué)成績(jī)升序顯示
mysql> select name, math from exam_result order by math asc;
+-----------+------+
| name | math |
+-----------+------+
| 宋公明 | 65 |
| 孫權(quán) | 73 |
| 孫悟空 | 78 |
| 曹孟德 | 84 |
| 劉玄德 | 85 |
| 唐三藏 | 98 |
| 豬悟能 | 98 |
+-----------+------+
7 rows in set (0.00 sec)
mysql>
- 同學(xué)及 qq 號(hào),按 qq 號(hào)排序顯示
-- NULL 視為比任何值都小,升序出現(xiàn)在最上面
mysql> select name, qq from students order by qq;
+-----------+-------+
| name | qq |
+-----------+-------+
| 唐玄奘 | NULL |
| 孫仲謀 | NULL |
| 曹阿瞞 | NULL |
| 孫悟空 | 11111 |
+-----------+-------+
4 rows in set (0.00 sec)
-- NULL 視為比任何值都小,降序出現(xiàn)在最下面
mysql> select name, qq from students order by qq desc;
+-----------+-------+
| name | qq |
+-----------+-------+
| 孫悟空 | 11111 |
| 唐玄奘 | NULL |
| 孫仲謀 | NULL |
| 曹阿瞞 | NULL |
+-----------+-------+
4 rows in set (0.00 sec)
mysql>
- 查詢(xún)同學(xué)各門(mén)成績(jī),依次按 數(shù)學(xué)降序,英語(yǔ)升序,語(yǔ)文升序的方式顯示
-- 多字段排序,排序優(yōu)先級(jí)隨書(shū)寫(xiě)順序
mysql> select name, chinese, math, english from exam_result order by math desc, english, chinese;
+-----------+---------+------+---------+
| name | chinese | math | english |
+-----------+---------+------+---------+
| 唐三藏 | 67 | 98 | 56 |
| 豬悟能 | 88 | 98 | 90 |
| 劉玄德 | 55 | 85 | 45 |
| 曹孟德 | 82 | 84 | 67 |
| 孫悟空 | 87 | 78 | 77 |
| 孫權(quán) | 70 | 73 | 78 |
| 宋公明 | 75 | 65 | 30 |
+-----------+---------+------+---------+
7 rows in set (0.00 sec)
mysql>
- 查詢(xún)同學(xué)及總分,由高到低
-- ORDER BY 中可以使用表達(dá)式
mysql> SELECT name, chinese + english + math FROM exam_result
-> ORDER BY chinese + english + math DESC;
+-----------+--------------------------+
| name | chinese + english + math |
+-----------+--------------------------+
| 豬悟能 | 276 |
| 孫悟空 | 242 |
| 曹孟德 | 233 |
| 唐三藏 | 221 |
| 孫權(quán) | 221 |
| 劉玄德 | 185 |
| 宋公明 | 170 |
+-----------+--------------------------+
7 rows in set (0.00 sec)
-- ORDER BY 子句中可以使用列別名
mysql> SELECT name, chinese + english + math 總分 FROM exam_result
-> ORDER BY 總分 DESC;
+-----------+--------+
| name | 總分 |
+-----------+--------+
| 豬悟能 | 276 |
| 孫悟空 | 242 |
| 曹孟德 | 233 |
| 唐三藏 | 221 |
| 孫權(quán) | 221 |
| 劉玄德 | 185 |
| 宋公明 | 170 |
+-----------+--------+
7 rows in set (0.00 sec)
mysql>
-- 原因在于最后排序是根據(jù)查詢(xún)出來(lái)的結(jié)果進(jìn)行排序的,即 select 比 order by 先執(zhí)行
- 查詢(xún)姓孫的同學(xué)或者姓曹的同學(xué)數(shù)學(xué)成績(jī),結(jié)果按數(shù)學(xué)成績(jī)由高到低顯示
-- 結(jié)合 WHERE 子句 和 ORDER BY 子句
mysql> select name, math from exam_result
-> where name like '孫%' or name like '曹%'
-> order by math desc;
+-----------+------+
| name | math |
+-----------+------+
| 曹孟德 | 84 |
| 孫悟空 | 78 |
| 孫權(quán) | 73 |
+-----------+------+
3 rows in set (0.00 sec)
mysql>
2.4 LIMIT 篩選結(jié)果分頁(yè)
2.4.1 分頁(yè)語(yǔ)法
-- 起始下標(biāo)為 0
-- 從 0 開(kāi)始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- 從 s 開(kāi)始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- 從 s 開(kāi)始,篩選 n 條結(jié)果,比第二種用法更明確,建議使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
建議:
對(duì)未知表進(jìn)行查詢(xún)時(shí),最好加一條 LIMIT 1,避免因?yàn)楸碇袛?shù)據(jù)過(guò)大,查詢(xún)?nèi)頂?shù)據(jù)導(dǎo)致數(shù)據(jù)庫(kù)卡死。
案例:
按 id 進(jìn)行分頁(yè),每頁(yè) 3 條記錄,分別顯示 第 1、2、3 頁(yè)
-- 第 1 頁(yè)
mysql> select id, name, chinese, math, english from exam_result order by id limit 3 offset 0;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孫悟空 | 87 | 78 | 77 |
| 3 | 豬悟能 | 88 | 98 | 90 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)
-- 第 2 頁(yè)
mysql> select id, name, chinese, math, english from exam_result order by id limit 3 offset 3;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 4 | 曹孟德 | 82 | 84 | 67 |
| 5 | 劉玄德 | 55 | 85 | 45 |
| 6 | 孫權(quán) | 70 | 73 | 78 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)
-- 第 3 頁(yè),如果結(jié)果不足 3 個(gè),不會(huì)有影響
mysql> select id, name, chinese, math, english from exam_result order by id limit 3 offset 6;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)
mysql>
2.4.2 分頁(yè)案例
三、Update操作
3.1 Update語(yǔ)法
語(yǔ)法:
UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...]
其作用是對(duì)查詢(xún)到的結(jié)果進(jìn)行列值更新。
3.2 案例
- 將孫悟空同學(xué)的數(shù)學(xué)成績(jī)變更為 80 分
-- 更新值為具體值
-- 查看原數(shù)據(jù)
mysql> select name, math from exam_result where name = '孫悟空';
+-----------+------+
| name | math |
+-----------+------+
| 孫悟空 | 78 |
+-----------+------+
1 row in set (0.00 sec)
-- 數(shù)據(jù)更新
mysql> update exam_result set math = 80 where name = '孫悟空';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- 查看更新后數(shù)據(jù)
mysql> select name, math from exam_result where name = '孫悟空';
+-----------+------+
| name | math |
+-----------+------+
| 孫悟空 | 80 |
+-----------+------+
1 row in set (0.00 sec)
- 將曹孟德同學(xué)的數(shù)學(xué)成績(jī)變更為 60 分,語(yǔ)文成績(jī)變更為 70 分
-- 一次更新多個(gè)列
-- 查看原數(shù)據(jù)
mysql> select name, math, chinese from exam_result where name = '曹孟德';
+-----------+------+---------+
| name | math | chinese |
+-----------+------+---------+
| 曹孟德 | 84 | 82 |
+-----------+------+---------+
1 row in set (0.00 sec)
-- 數(shù)據(jù)更新
mysql> update exam_result set math = 60, chinese = 70 where name = '曹孟德';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- 查看更新后數(shù)據(jù)
mysql> select name, math, chinese from exam_result where name = '曹孟德';
+-----------+------+---------+
| name | math | chinese |
+-----------+------+---------+
| 曹孟德 | 60 | 70 |
+-----------+------+---------+
1 row in set (0.00 sec)
- 將總成績(jī)倒數(shù)前三的 3 位同學(xué)的數(shù)學(xué)成績(jī)加上 30 分
-- 更新值為原值基礎(chǔ)上變更
-- 查看原數(shù)據(jù)
-- 別名可以在ORDER BY中使用
mysql> select name, math, chinese + math + english 總分 from exam_result order by 總分 limit 3;
+-----------+------+--------+
| name | math | 總分 |
+-----------+------+--------+
| 宋公明 | 65 | 170 |
| 劉玄德 | 85 | 185 |
| 曹孟德 | 60 | 197 |
+-----------+------+--------+
3 rows in set (0.00 sec)
-- 數(shù)據(jù)更新,不支持 math += 30 這種語(yǔ)法
mysql> update exam_result set math = math + 30 order by chinese + math + english limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
-- 查看更新后數(shù)據(jù)
-- 注意:這里不能在按照總分前三來(lái)查看更新后的數(shù)據(jù),因?yàn)榕琶呀?jīng)發(fā)生變化
mysql> select name, math, chinese + math + english 總分 from exam_result where name in ('宋公明', '劉玄德', '曹孟德');
+-----------+------+--------+
| name | math | 總分 |
+-----------+------+--------+
| 曹孟德 | 90 | 227 |
| 劉玄德 | 115 | 215 |
| 宋公明 | 95 | 200 |
+-----------+------+--------+
3 rows in set (0.00 sec)
- 將所有同學(xué)的語(yǔ)文成績(jī)更新為原來(lái)的 2 倍
注意:更新全表的語(yǔ)句慎用!
-- 沒(méi)有 WHERE 子句,則更新全表
-- 查看原數(shù)據(jù)
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孫悟空 | 87 | 80 | 77 |
| 3 | 豬悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 70 | 90 | 67 |
| 5 | 劉玄德 | 55 | 115 | 45 |
| 6 | 孫權(quán) | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 95 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
-- 數(shù)據(jù)更新
mysql> update exam_result set chinese = chinese * 2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7 Changed: 7 Warnings: 0
-- 查看更新后數(shù)據(jù)
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 134 | 98 | 56 |
| 2 | 孫悟空 | 174 | 80 | 77 |
| 3 | 豬悟能 | 176 | 98 | 90 |
| 4 | 曹孟德 | 140 | 90 | 67 |
| 5 | 劉玄德 | 110 | 115 | 45 |
| 6 | 孫權(quán) | 140 | 73 | 78 |
| 7 | 宋公明 | 150 | 95 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
四、Delete操作
4.1 刪除數(shù)據(jù)
語(yǔ)法:
DELETE FROM table_name [WHERE …] [ORDER BY …] [LIMIT …]
案例:
- 刪除孫悟空同學(xué)的考試成績(jī)
-- 查看原數(shù)據(jù)
mysql> select * from exam_result where name = '孫悟空';
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 2 | 孫悟空 | 174 | 80 | 77 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)
-- 刪除數(shù)據(jù)
mysql> delete from exam_result where name = '孫悟空';
Query OK, 1 row affected (0.00 sec)
-- 查看刪除結(jié)果
mysql> select * from exam_result where name = '孫悟空';
Empty set (0.00 sec)
- 刪除整張表數(shù)據(jù)
注意:刪除整表操作要慎用!
-- 準(zhǔn)備測(cè)試表
CREATE TABLE for_delete (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20)
);
-- 插入測(cè)試數(shù)據(jù)
INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
-- 查看測(cè)試數(shù)據(jù)
mysql> SELECT * FROM for_delete;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
-- 刪除整表數(shù)據(jù)
mysql> DELETE FROM for_delete;
Query OK, 3 rows affected (0.00 sec)
-- 查看刪除結(jié)果
mysql> SELECT * FROM for_delete;
Empty set (0.00 sec)
-- 再插入一條數(shù)據(jù),自增 id 在原值上增長(zhǎng)
mysql> INSERT INTO for_delete (name) VALUES ('D');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM for_delete;
+----+------+
| id | name |
+----+------+
| 4 | D |
+----+------+
1 row in set (0.00 sec)
-- 查看表結(jié)構(gòu),會(huì)有 AUTO_INCREMENT=n 項(xiàng)
mysql> SHOW CREATE TABLE for_delete\G
*************************** 1. row ***************************
Table: for_delete
Create Table: CREATE TABLE `for_delete` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
4.2 截?cái)啾?/h3>
語(yǔ)法:
TRUNCATE [TABLE] table_name
注意:這個(gè)操作慎用
- 只能對(duì)整表操作,不能像 DELETE 一樣針對(duì)部分?jǐn)?shù)據(jù)操作;
- 實(shí)際上 MySQL 不對(duì)數(shù)據(jù)操作,所以比 DELETE 更快,但是TRUNCATE在刪除數(shù)據(jù)的時(shí)候,并不經(jīng)過(guò)真正的事務(wù),所以無(wú).法回滾
- 會(huì)重置 AUTO_INCREMENT 項(xiàng)
案例:
-- 準(zhǔn)備測(cè)試表
CREATE TABLE for_truncate (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20)
);
-- 插入測(cè)試數(shù)據(jù)
INSERT INTO for_truncate (name) VALUES ('A'), ('B'), ('C');
-- 查看測(cè)試數(shù)據(jù)
SELECT * FROM for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
-- 截?cái)嗾頂?shù)據(jù),注意影響行數(shù)是 0,所以實(shí)際上沒(méi)有對(duì)數(shù)據(jù)真正操作
TRUNCATE for_truncate;
Query OK, 0 rows affected (0.10 sec)
-- 查看刪除結(jié)果
SELECT * FROM for_truncate;
Empty set (0.00 sec)
-- 再插入一條數(shù)據(jù),自增 id 在重新增長(zhǎng)
INSERT INTO for_truncate (name) VALUES ('D');
Query OK, 1 row affected (0.00 sec)
-- 查看數(shù)據(jù)
SELECT * FROM for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | D |
+----+------+
1 row in set (0.00 sec)
-- 查看表結(jié)構(gòu),會(huì)有 AUTO_INCREMENT=2 項(xiàng)
SHOW CREATE TABLE for_truncate\G
*************************** 1. row ***************************
Table: for_truncate
Create Table: CREATE TABLE `for_truncate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
五、插入查詢(xún)結(jié)果
語(yǔ)法:
INSERT INTO table_name [(column [, column …])] SELECT …
案例:刪除表中的的重復(fù)復(fù)記錄,重復(fù)的數(shù)據(jù)只能有一份
-- 創(chuàng)建原數(shù)據(jù)表
mysql> CREATE TABLE duplicate_table (id int, name varchar(20));
Query OK, 0 rows affected (0.01 sec)
-- 插入測(cè)試數(shù)據(jù)
mysql> INSERT INTO duplicate_table VALUES
-> (100, 'aaa'),
-> (100, 'aaa'),
-> (200, 'bbb'),
-> (200, 'bbb'),
-> (200, 'bbb'),
-> (300, 'ccc');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
-- 創(chuàng)建一張空表 no_duplicate_table,結(jié)構(gòu)和 duplicate_table 一樣
mysql> CREATE TABLE no_duplicate_table LIKE duplicate_table;
Query OK, 0 rows affected (0.01 sec)
-- 將 duplicate_table 的去重?cái)?shù)據(jù)插入到 no_duplicate_table
mysql> INSERT INTO no_duplicate_table SELECT DISTINCT * FROM duplicate_table;
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
-- 通過(guò)重命名表,實(shí)現(xiàn)原子的去重操作
mysql> RENAME TABLE duplicate_table TO old_duplicate_table, no_duplicate_table TO duplicate_table;
Query OK, 0 rows affected (0.01 sec)
-- 查看最終結(jié)果
mysql> SELECT * FROM duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
+------+------+
3 rows in set (0.00 sec)
六、聚合函數(shù)
6.1 聚合函數(shù)分類(lèi)
函數(shù) | 說(shuō)明 |
---|---|
COUNT([DISTINCT] expr) | 返回查詢(xún)到的數(shù)據(jù)的數(shù)量 |
SUM([DISTINCT] expr) | 返回查詢(xún)到的數(shù)據(jù)的總和,不是數(shù)字沒(méi)有意義 |
AVG([DISTINCT] expr) | 返回查詢(xún)到的數(shù)據(jù)的平均值,不是數(shù)字沒(méi)有意義 |
MAX([DISTINCT] expr) | 返回查詢(xún)到的數(shù)據(jù)的最大值,不是數(shù)字沒(méi)有意義 |
MIN([DISTINCT] expr) | 返回查詢(xún)到的數(shù)據(jù)的最小值,不是數(shù)字沒(méi)有意義 |
6.2 案例
- 統(tǒng)計(jì)班級(jí)共有多少同學(xué)
-- 使用 * 做統(tǒng)計(jì),不受 NULL 影響
mysql> select count(*) from students;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
-- 使用表達(dá)式做統(tǒng)計(jì)
mysql> select count(1) from students;
+----------+
| count(1) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
- 統(tǒng)計(jì)班級(jí)收集的 qq 號(hào)有多少
-- NULL 不會(huì)計(jì)入結(jié)果
mysql> select count(qq) from students;
+-----------+
| count(qq) |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
- 統(tǒng)計(jì)本次考試的數(shù)學(xué)成績(jī)分?jǐn)?shù)個(gè)數(shù)
-- COUNT(math) 統(tǒng)計(jì)的是全部成績(jī)
mysql> select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
| 6 |
+-------------+
1 row in set (0.00 sec)
-- COUNT(DISTINCT math) 統(tǒng)計(jì)的是去重成績(jī)數(shù)量
mysql> select count(distinct math) from exam_result;
+----------------------+
| count(distinct math) |
+----------------------+
| 5 |
+----------------------+
1 row in set (0.00 sec)
- 統(tǒng)計(jì)數(shù)學(xué)成績(jī)總分
mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
| 569 |
+-----------+
1 row in set (0.00 sec)
-- 不及格 < 60 的總分,沒(méi)有結(jié)果,返回 NULL
mysql> select sum(math) from exam_result where math < 60;
+-----------+
| sum(math) |
+-----------+
| NULL |
+-----------+
1 row in set (0.00 sec)
- 統(tǒng)計(jì)平均總分
mysql> select avg(chinese + math + english) 平均總分 from exam_result;
+--------------+
| 平均總分 |
+--------------+
| 297.5 |
+--------------+
1 row in set (0.00 sec)
- 返回英語(yǔ)最高分
mysql> select max(english) from exam_result;
+--------------+
| max(english) |
+--------------+
| 90 |
+--------------+
1 row in set (0.00 sec)
- 返回 > 70 分以上的數(shù)學(xué)最低分
mysql> select min(math) from exam_result where math > 70;
+-----------+
| min(math) |
+-----------+
| 73 |
+-----------+
1 row in set (0.00 sec)
七、GROUP BY 子句的使用
在select中使用group by 子句可以對(duì)指定列進(jìn)行分組查詢(xún)。
語(yǔ)法:
select column1, column2, … from table group by column;文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-432904.html
案例:
- 準(zhǔn)備工作,創(chuàng)建一個(gè)雇員信息表(來(lái)自oracle 9i的經(jīng)典測(cè)試表)
- EMP員工表
- DEPT部門(mén)表
- SALGRADE工資等級(jí)表
DROP database IF EXISTS `scott`;
CREATE database IF NOT EXISTS `scott` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `scott`;
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`deptno` int(2) unsigned zerofill NOT NULL COMMENT '部門(mén)編號(hào)',
`dname` varchar(14) DEFAULT NULL COMMENT '部門(mén)名稱(chēng)',
`loc` varchar(13) DEFAULT NULL COMMENT '部門(mén)所在地點(diǎn)'
);
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`empno` int(6) unsigned zerofill NOT NULL COMMENT '雇員編號(hào)',
`ename` varchar(10) DEFAULT NULL COMMENT '雇員姓名',
`job` varchar(9) DEFAULT NULL COMMENT '雇員職位',
`mgr` int(4) unsigned zerofill DEFAULT NULL COMMENT '雇員領(lǐng)導(dǎo)編號(hào)',
`hiredate` datetime DEFAULT NULL COMMENT '雇傭時(shí)間',
`sal` decimal(7,2) DEFAULT NULL COMMENT '工資月薪',
`comm` decimal(7,2) DEFAULT NULL COMMENT '獎(jiǎng)金',
`deptno` int(2) unsigned zerofill DEFAULT NULL COMMENT '部門(mén)編號(hào)'
);
DROP TABLE IF EXISTS `salgrade`;
CREATE TABLE `salgrade` (
`grade` int(11) DEFAULT NULL COMMENT '等級(jí)',
`losal` int(11) DEFAULT NULL COMMENT '此等級(jí)最低工資',
`hisal` int(11) DEFAULT NULL COMMENT '此等級(jí)最高工資'
);
- 如何顯示每個(gè)部門(mén)的平均工資和最高工資
select deptno,avg(sal),max(sal) from EMP group by deptno;
- 顯示每個(gè)部門(mén)的每種崗位的平均工資和最低工資
select avg(sal),min(sal),job, deptno from EMP group by deptno, job;
- 顯示平均工資低于2000的部門(mén)和它的平均工資
- 統(tǒng)計(jì)各個(gè)部門(mén)的平均工資
select avg(sal) from EMP group by deptno;
- having和group by配合使用,對(duì)group by結(jié)果進(jìn)行過(guò)濾
select avg(sal) as myavg from EMP group by deptno having myavg<2000;
--having經(jīng)常和group by搭配使用,作用是對(duì)分組進(jìn)行篩選,作用有些像where。
到了這里,關(guān)于【MySQL學(xué)習(xí)】MySQL表的增刪改查操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!