一.數據表本身的基本操作
1.1新建表
1.2查看表
1.3查看表結構
1.4刪除表
二.數據表的增刪查改(CRUD)
2.1插入
2.2查詢
2.2.1查詢表中所有元素(查詢整表)
2.2.2指定列查詢
2.2.3指定字段為表達式查詢
2.2.4指定別名字段查詢
2.2.5去重查詢
2.2.6排序查詢
2.2.7條件查詢
2.2.8分頁查詢
2.3修改
2.4刪除
1.數據表本身的基本操作
1.1新建表
在選定數據庫后就可以進行數據表的創建了,語法:
create table 表名(定義列1, 定義列2, .......);--具體如下
create table 表名(變量名1 數據類型1, 變量名2 數據類型2, .......);
例如創建一個學生表:
MySQL> create table student(id int, name varchar(50),score double(3,1));
Query OK, 0 rows affected (0.03 sec)
1.2查看表
查看某數據庫中有哪些數據表,語法:
show tables;
栗子:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| student |
| test_1 |
+----------------+
2 rows in set (0.00 sec)
1.3查看表結構
查看一個數據表中有哪一些列(字段),語法:
desc 表名;
查看學生表結構:
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| score | double(3,1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
1.4刪除表
(慎重操作),對數據表整體進行刪除,語法:
drop table 表名;
栗子:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| student |
| test_1 |
+----------------+
2 rows in set (0.00 sec)
mysql> drop table test_1;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| student |
+----------------+
1 row in set (0.00 sec)
2.數據表的增刪查改(CRUD)
2.1插入
往表里插入一行數據,語法:
insert into 表名 values (對應列的實參列表);
其中values后面()里面的值列表要與創建表時的列一一對應。
如在學生表中插入一行:
mysql> insert into student values (1, "喜羊羊", 98);
Query OK, 1 row affected (0.01 sec)
當然也可以指定列插入,語法:
insert into 表名 (需要插入的列) values (對應列的實參列表);
沒有指定的列為默認值。
栗子:
mysql> insert into student (id, name) values (2, "懶羊羊");
Query OK, 1 row affected (0.00 sec)
當然也可以多行插入,格式:
insert into 表名 values (對應列的實參列表1), (對應列的實參列表2), ...;
栗子:
mysql> insert into student values (3,"美羊羊", 92), (4, "沸羊羊", 70), (5, "暖羊羊", 88);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
有關日期數據的插入,可以使用datetime數據類型,其中now()能夠獲取當前日期。
該類型日期的格式為:yyyy-mm-dd hh:mm:ss
栗子:
mysql> create table time(id int, time datetime);
Query OK, 0 rows affected (0.02 sec)
mysql> desc time;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| time | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into time values (1, '2022-03-12');
Query OK, 1 row affected (0.00 sec)
mysql> insert into time values (2, '2022-03-12 20:22:22');
Query OK, 1 row affected (0.00 sec)
mysql> insert into time values (3, now());
Query OK, 1 row affected (0.01 sec)
2.2查詢
上面我們已經介紹數據庫中表的查詢和表中元素的插入等操作,上面所演示的代碼中只提示插入成功,但是我們并不能看見它是否真的插入進去了,所以需要對表進行查詢,數據表的查詢是數據表增刪查改中最復雜的一項操作。
2.2.1查詢表中所有元素(查詢整表)
該操作要慎重使用,因為MySQL數據都儲存在服務器的硬盤上,查詢操作是將服務器中的數據返回給客戶端,由于在企業級的數據庫中,數據量非常的大,是TB級別的,一旦使用整表查詢,會把服務器吃滿,此時其他客戶端訪問服務器時,服務器無法及時作出響應。
語法:
select * from 表名;
其中上面的*表示通配符,表示一個表所有的列。
栗子,查詢上面新建的學生表和日期表。
mysql> insert into time values (3, now());
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+------+-----------+-------+
| id | name | score |
+------+-----------+-------+
| 1 | 喜羊羊 | 98.0 |
| 2 | 懶羊羊 | NULL |
| 3 | 美羊羊 | 92.0 |
| 4 | 沸羊羊 | 70.0 |
| 5 | 暖羊羊 | 88.0 |
+------+-----------+-------+
5 rows in set (0.00 sec)
mysql> select * from time;
+------+---------------------+
| id | time |
+------+---------------------+
| 1 | 2022-03-12 00:00:00 |
| 2 | 2022-03-12 20:22:22 |
| 3 | 2022-03-12 20:13:39 |
+------+---------------------+
3 rows in set (0.00 sec)
這個輸出顯示的表是服務器端數據庫返回給客戶端的一個臨時表,使用查詢操作不會對服務器中的數據造成影響。且該臨時表在客戶端打印后也隨之銷毀了。
2.2.2指定列查詢
指定列查詢后,服務器返回的數據量會大大減少,因此查詢一般指定列來進行查詢。
語法格式:
select 列名, 列名, ... from 表名;
比如我們只查詢學生表中的姓名和成績:
mysql> select name, score from student;
+-----------+-------+
| name | score |
+-----------+-------+
| 喜羊羊 | 98.0 |
| 懶羊羊 | NULL |
| 美羊羊 | 92.0 |
| 沸羊羊 | 70.0 |
| 暖羊羊 | 88.0 |
+-----------+-------+
5 rows in set (0.00 sec)
2.2.3指定字段為表達式查詢
如果我們期望對數據表中的某列統一加上一個權重進行查詢,我們可以指定列的表達式來查詢,比如對學生表的成績統一減少10分進行查詢:
mysql> select name, score-10 from student;
+-----------+----------+
| name | score-10 |
+-----------+----------+
| 喜羊羊 | 88.0 |
| 懶羊羊 | NULL |
| 美羊羊 | 82.0 |
| 沸羊羊 | 60.0 |
| 暖羊羊 | 78.0 |
+-----------+----------+
5 rows in set (0.00 sec)
我們發現null不受影響。
我們重新再建一個表,該表里有學號,姓名,語文成績,數學成績,英語成績,計算機綜合成績,滿分都為100分。
我們現在需要統計表中所有同學的總成績和每個學生的平均分。
-- 建表
mysql> create table exam_score(id int, name varchar(50), chinese double(4,1), math double(4,1),english double(4,1), computer double(4,1));
Query OK, 0 rows affected (0.02 sec)
-- 插入數據
mysql> insert into exam_score values (1,'美羊羊',99.5,90.5,98,82),
-> (2, '懶羊羊', 58.5,32.5,24,66.5),
-> (3, '喜羊羊', 92,98,88,100),
-> (4, '沸羊羊', 78,72,74.5,81),
-> (5, '暖羊羊', 90,91,98,76),
-> (6, '灰太狼', 33,91,12,98.5),
-> (7, '小灰灰', 81, 82,78,88),
-> (8, '黑大帥', 10,11,9,20);
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0
-- 整表查詢
mysql> select * from exam_score;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懶羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
| 8 | 黑大帥 | 10.0 | 11.0 | 9.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
8 rows in set (0.00 sec)
-- 統計每一位同學的總分與平均分
mysql> select name, chinese+math+english+computer,(chinese+math+english+computer)/4 from exam_score;
+-----------+-------------------------------+-----------------------------------+
| name | chinese+math+english+computer | (chinese+math+english+computer)/4 |
+-----------+-------------------------------+-----------------------------------+
| 美羊羊 | 370.0 | 92.50000 |
| 懶羊羊 | 181.5 | 45.37500 |
| 喜羊羊 | 378.0 | 94.50000 |
| 沸羊羊 | 305.5 | 76.37500 |
| 暖羊羊 | 355.0 | 88.75000 |
| 灰太狼 | 234.5 | 58.62500 |
| 小灰灰 | 329.0 | 82.25000 |
| 黑大帥 | 50.0 | 12.50000 |
+-----------+-------------------------------+-----------------------------------+
8 rows in set (0.00 sec)
2.2.4指定別名字段查詢
上面查詢總成績與平均成績時,輸出的表頭是一個表達式,不是非常合適作為表頭標題,那能不能修改別名呢?答案是肯定的。
語法格式:
select 列名或表達式 as 別名, ... from 表名;
其中as可以省略,但是不建議這樣做,因為可讀性會變差。
以上面的考試成績表為例,
mysql> select name, chinese+math+english+computer as total,(chinese+math+english+computer)/4 as average from exam_score;
+-----------+-------+----------+
| name | total | average |
+-----------+-------+----------+
| 美羊羊 | 370.0 | 92.50000 |
| 懶羊羊 | 181.5 | 45.37500 |
| 喜羊羊 | 378.0 | 94.50000 |
| 沸羊羊 | 305.5 | 76.37500 |
| 暖羊羊 | 355.0 | 88.75000 |
| 灰太狼 | 234.5 | 58.62500 |
| 小灰灰 | 329.0 | 82.25000 |
| 黑大帥 | 50.0 | 12.50000 |
+-----------+-------+----------+
8 rows in set (0.00 sec)
要注意一件事,前面指定的列的別名,不能用于其他指定列或表達式中,比如像這樣就會報錯。
mysql> select name, chinese+math+english+computer as total,total/4 as average from exam_score;
ERROR 1054 (42S22): Unknown column 'total' in 'field list'
2.2.5去重查詢
MySQL中支持對列的查詢,比如上面的英語成績,美羊羊和暖羊羊都是98分,只對英語這一列成績查詢時可以通過關鍵字distinct進行去重查詢。
mysql> select distinct english from exam_score;
+---------+
| english |
+---------+
| 98.0 |
| 24.0 |
| 88.0 |
| 74.5 |
| 12.0 |
| 78.0 |
| 9.0 |
+---------+
7 rows in set (0.00 sec)
看,結果少了一個98,達到了去重的效果。
2.2.6排序查詢
可以針對查詢結果對數據進行排序,得到臨時表輸出,語法格式:
select 列名,... from 表名 order by 列名(需排序)asc(或者desc);
最后的asc表示升序,也是默認排序方式,desc是降序排序。
例如,將上面的成績按照總分進行升序和降序排名。
-- 升序
mysql> select name, chinese+math+english+computer as total,(chinese+math+english+computer)/4 as average from exam_score order by total asc;
+-----------+-------+----------+
| name | total | average |
+-----------+-------+----------+
| 黑大帥 | 50.0 | 12.50000 |
| 懶羊羊 | 181.5 | 45.37500 |
| 灰太狼 | 234.5 | 58.62500 |
| 沸羊羊 | 305.5 | 76.37500 |
| 小灰灰 | 329.0 | 82.25000 |
| 暖羊羊 | 355.0 | 88.75000 |
| 美羊羊 | 370.0 | 92.50000 |
| 喜羊羊 | 378.0 | 94.50000 |
+-----------+-------+----------+
8 rows in set (0.00 sec)
-- 降序
mysql> select name, chinese+math+english+computer as total,(chinese+math+english+computer)/4 as average from exam_score order by total desc;
+-----------+-------+----------+
| name | total | average |
+-----------+-------+----------+
| 喜羊羊 | 378.0 | 94.50000 |
| 美羊羊 | 370.0 | 92.50000 |
| 暖羊羊 | 355.0 | 88.75000 |
| 小灰灰 | 329.0 | 82.25000 |
| 沸羊羊 | 305.5 | 76.37500 |
| 灰太狼 | 234.5 | 58.62500 |
| 懶羊羊 | 181.5 | 45.37500 |
| 黑大帥 | 50.0 | 12.50000 |
+-----------+-------+----------+
8 rows in set (0.00 sec)
如果asc或者desc省略,則按照升序排列。
mysql> select name, chinese+math+english+computer as total,(chinese+math+english+computer)/4 as average from exam_score order by total;
+-----------+-------+----------+
| name | total | average |
+-----------+-------+----------+
| 黑大帥 | 50.0 | 12.50000 |
| 懶羊羊 | 181.5 | 45.37500 |
| 灰太狼 | 234.5 | 58.62500 |
| 沸羊羊 | 305.5 | 76.37500 |
| 小灰灰 | 329.0 | 82.25000 |
| 暖羊羊 | 355.0 | 88.75000 |
| 美羊羊 | 370.0 | 92.50000 |
| 喜羊羊 | 378.0 | 94.50000 |
+-----------+-------+----------+
8 rows in set (0.00 sec)
如果出現null,則默認是最小的。
排序時,可以針對多列排序,這樣排序是有優先級的,優先級按照從左至右,比如先針對英語成績排序,再針對數學成績排序,意思是先比較英語,當英語成績相同的情況下再比較數學。
mysql> select * from exam_score order by english desc, math desc;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 2 | 懶羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
| 8 | 黑大帥 | 10.0 | 11.0 | 9.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
8 rows in set (0.00 sec)
2.2.7條件查詢
所謂條件查詢,就是給定一個條件,如果滿足就那個查詢出來,在MySQL中使用關鍵字where+條件來設定條件,條件查詢語法格式如下:
select 列名, ... from 表名 where 條件;
這個所謂的條件就相當于JAVA中的boolean表達式,首先來認識一下MySQL里面的運算符。
比較大小:>, <, <=, >=,=(判斷是否相等,但是不能比較NULL), <=>(判斷是否相等,能比較NULL)。
劃定區間:between a and b;表示閉區間[a, b]。
判斷是否在指定的幾個值之中存在:in(a,b,c,...);判定結果是否在a,b,c,…之中。
判斷是否為NULL:is null。
判斷是否不為NULL:is not null。
模糊匹配:like,需要配合通配符%或者-來使用。
通配符:%表示任意個數量的字符,_表示任意的一個字符。
邏輯運算符:and(&&), or(||), not(!)。
例如查詢數學不及格同學名單:
mysql> select * from exam_score where math < 60;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 2 | 懶羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 8 | 黑大帥 | 10.0 | 11.0 | 9.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
2 rows in set (0.00 sec)
查詢英語成績大于計算機綜合的同學:
mysql> select name, english, computer from exam_score where english > computer;
+-----------+---------+----------+
| name | english | computer |
+-----------+---------+----------+
| 美羊羊 | 98.0 | 82.0 |
| 暖羊羊 | 98.0 | 76.0 |
+-----------+---------+----------+
2 rows in set (0.00 sec)
查詢總分在300分以下的同學:
mysql> select name, chinese+math+english+computer as total from exam_score where chinese+math+english+computer < 300;
+-----------+-------+
| name | total |
+-----------+-------+
| 懶羊羊 | 181.5 |
| 灰太狼 | 234.5 |
| 黑大帥 | 50.0 |
+-----------+-------+
3 rows in set (0.00 sec)
注意使用別名時,條件表達式不能使用別名判斷,否則會報錯:
mysql> select name, chinese+math+english+computer as total from exam_score where total < 300;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
查詢語文數學均大于等于90分的同學:
mysql> select name, chinese, math from exam_score where chinese >= 90 and math >= 90;
+-----------+---------+------+
| name | chinese | math |
+-----------+---------+------+
| 美羊羊 | 99.5 | 90.5 |
| 喜羊羊 | 92.0 | 98.0 |
| 暖羊羊 | 90.0 | 91.0 |
+-----------+---------+------+
3 rows in set (0.00 sec)
查詢語數英中有大于等于95分的同學:
mysql> select name, chinese, math, english from exam_score where chinese >= 95 or math >= 95 or english >= 95;
+-----------+---------+------+---------+
| name | chinese | math | english |
+-----------+---------+------+---------+
| 美羊羊 | 99.5 | 90.5 | 98.0 |
| 喜羊羊 | 92.0 | 98.0 | 88.0 |
| 暖羊羊 | 90.0 | 91.0 | 98.0 |
+-----------+---------+------+---------+
3 rows in set (0.00 sec)
查詢語文數學總分大于等于180分或者計算機綜合與英語大于85分的同學:
mysql> select name, chinese+math as cmtotal, english, computer from exam_score where chinese+math >= 180 or computer > 85 and english > 85;
+-----------+---------+---------+----------+
| name | cmtotal | english | computer |
+-----------+---------+---------+----------+
| 美羊羊 | 190.0 | 98.0 | 82.0 |
| 喜羊羊 | 190.0 | 88.0 | 100.0 |
| 暖羊羊 | 181.0 | 98.0 | 76.0 |
+-----------+---------+---------+----------+
3 rows in set (0.00 sec)
從上述代碼我們能夠看出來and的優先級大于or,如果需要打破這個優先級需要使用()。
查詢計算機綜合在[80, 90]之間的同學:
mysql> select name, computer from exam_score where computer between 80 and 90;
+-----------+----------+
| name | computer |
+-----------+----------+
| 美羊羊 | 82.0 |
| 沸羊羊 | 81.0 |
| 小灰灰 | 88.0 |
+-----------+----------+
-- 等價于
mysql> select name, computer from exam_score where computer >= 80 and computer <= 90;
+-----------+----------+
| name | computer |
+-----------+----------+
| 美羊羊 | 82.0 |
| 沸羊羊 | 81.0 |
| 小灰灰 | 88.0 |
+-----------+----------+
3 rows in set (0.00 sec)
查詢計算機綜合成績為82,88,92,98的同學:
mysql> select name, computer from exam_score where computer in(82,88,92,98);
+-----------+----------+
| name | computer |
+-----------+----------+
| 美羊羊 | 82.0 |
| 小灰灰 | 88.0 |
+-----------+----------+
2 rows in set (0.00 sec)
-- 等價于
mysql> select name, computer from exam_score where computer=82 or computer=88 or computer=92 or computer=98;
+-----------+----------+
| name | computer |
+-----------+----------+
| 美羊羊 | 82.0 |
| 小灰灰 | 88.0 |
+-----------+----------+
2 rows in set (0.00 sec)
查找名字含有羊的同學:
mysql> select * from exam_score where name like '%羊';
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懶羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
+------+-----------+---------+------+---------+----------+
5 rows in set (0.00 sec)
這里如果使用需要這樣寫,因為只能替代一個任意字符而%能替代任意個任意字符(包括0個)。
mysql> select * from exam_score where name like '__羊';
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懶羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
+------+-----------+---------+------+---------+----------+
5 rows in set (0.00 sec)
2.2.8分頁查詢
我們在使用瀏覽器搜索時會搜索出大量的數據,一般搜出幾十幾百萬條數據是很常見的,我們所瀏覽的電腦頁面是不可能將所有的內容都呈現在一個頁面,而我們拉到搜索頁面的最下面會發現一個頁碼:
搜索引擎將所有的數據結果按頁進行呈現,而且我們點進下一頁時需要重新加載,說明搜索引擎不是一次性把所有的結果全部加載好,而是一頁一頁地將結果呈現出來,像這種模式的搜索或者查詢就是分頁查詢,SQL也支持分頁查詢,使用關鍵字limit和offset實現。
語法格式:
select 列,列,列,... from 表名 limit 結果個數;
比如以每頁最多3個元素查詢考試成績表:
mysql> select * from exam_score limit 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懶羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
這個表后面的元素,可以指定一個下標(從0開始),以該下標對應的搜索結果為該頁的首個結果依次輸出。
語法格式:
select 列,列,列,... from 表名 limit 結果個數 offset 起始下標;
考試成績表第二頁結果:
mysql> select * from exam_score limit 3 offset 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
剩下的結果:
mysql> select * from exam_score limit 3 offset 6;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
| 8 | 黑大帥 | 10.0 | 11.0 | 9.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
2 rows in set (0.00 sec)
分頁查詢本質上就是限制查詢結果的條數,你可以設置最多輸出幾條結果,同時也可以指定從哪一條結果開始輸出。
此外分頁查詢也支持嵌套排序查找以及搭配條件等其他語句一起使用。
輸出成績表語文成績前三名:
mysql> select * from exam_score order by chinese desc limit 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
查詢總結:
整表查詢是一個非常危險的操作,要慎重使用,為了降低查詢的危險性,一般需要搭配where語句使用,或者直接使用limit限制。
2.3修改
數據表的修改操作格式語法:
update 表名 set 列名 = 值, 列名 = 值 where 需要修改元素的條件;
該操作除了where,order by 和 limit也可以使用。
比如,對于上述成績表,喜羊羊的英語成績登記錯了,需要將喜羊羊的英語成績修改為92。
mysql> update exam_score set english = 92 where name = "喜羊羊";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from exam_score where name = "喜羊羊";
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 3 | 喜羊羊 | 92.0 | 98.0 | 92.0 | 100.0 |
+------+-----------+---------+------+---------+----------+
1 row in set (0.00 sec)
由于修改操作不可逆,而且如果修改條件不嚴格,可能會造成大量數據被修改,因此該操作也是比較危險的操作,要慎重。
另外NULL值進行運算結果仍為NULL。
再來練習一下修改操作,分別將總分成績倒數第一,倒數第二,倒數第三的同學英語成績加上15分。
-- 查詢倒數前三的名單
mysql> select * from exam_score order by chinese+math+english+computer limit 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 8 | 黑大帥 | 10.0 | 11.0 | 9.0 | 20.0 |
| 2 | 懶羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
-- 修改
mysql> select * from exam_score order by chinese+math+english+computer limit 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 8 | 黑大帥 | 10.0 | 11.0 | 9.0 | 20.0 |
| 2 | 懶羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
mysql> update exam_score set english=english+20 order by chinese+math+english+computer limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
-- 查看新結果
mysql> select * from exam_score where name="黑大帥" or name="懶羊羊" or name="灰太狼";
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 2 | 懶羊羊 | 58.5 | 32.5 | 44.0 | 66.5 |
| 6 | 灰太狼 | 33.0 | 91.0 | 32.0 | 98.5 |
| 8 | 黑大帥 | 10.0 | 11.0 | 29.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
2.4刪除
格式語法:
delete from 表名 where 需要刪除的條件;
也可以嵌套order by和limit語句。
例如,刪除黑大帥的成績。
-- 刪除前
mysql> select * from exam_score;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懶羊羊 | 58.5 | 32.5 | 44.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 92.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 6 | 灰太狼 | 33.0 | 91.0 | 32.0 | 98.5 |
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
| 8 | 黑大帥 | 10.0 | 11.0 | 29.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
8 rows in set (0.00 sec)
-- 刪除中
mysql> delete from exam_score where name="黑大帥";
Query OK, 1 row affected (0.00 sec)
-- 刪除后
mysql> select * from exam_score;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懶羊羊 | 58.5 | 32.5 | 44.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 92.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 6 | 灰太狼 | 33.0 | 91.0 | 32.0 | 98.5 |
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
+------+-----------+---------+------+---------+----------+
7 rows in set (0.00 sec)
該操作危險,要慎重使用。
MySQL有關數據表的基本增刪查改的內容就全部介紹完畢了,還有一些更高級的CRUD操作博主后續也會安排上的
原文鏈接:
https://blog.csdn.net/m0_59139260/article/details/123281542