MySQL表的增刪改查(CRUD)
??一. CRUD
- CRUD : Create,Retrieve,Update,Delete
- 新增數(shù)據(jù)
- 查詢數(shù)據(jù)
- 修改數(shù)據(jù)
- 刪除數(shù)據(jù)
- MySQL的工作就是組織管理數(shù)據(jù),先保存,保存好了后好進行增刪改查
增刪改查的前提是已經(jīng)把數(shù)據(jù)庫創(chuàng)建好,并且選中了,表也創(chuàng)建就緒
- 注釋:在SQL中可以使用“–空格+描述”來表示注釋說明
- CRUD 即增加(Create)、查詢(Retrieve)、更新(Update)、刪除(Delete)四個單詞的首字母縮寫
??二. 新增(Create)
insert into 表名 values(值,值,值…);
- 注意此處的值的個數(shù)要和表的列數(shù)匹配,值的類型也要和列的類型匹配(不匹配就會報錯?。。。?/span>
所以也更好的體現(xiàn)出關系型數(shù)據(jù)庫的一個優(yōu)勢:對數(shù)據(jù)進行更嚴格的校驗檢查,更容易發(fā)現(xiàn)問題!
1.我們先在庫里創(chuàng)建一個學生表:
mysql> create table student(id int, name varchar(20)); Query OK, 0 rows affected (0.01 sec)
2.查看表的結構
mysql> desc student; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
3.新增
mysql> insert into student values (1,"zhangsan"); Query OK, 1 row affected (0.00 sec)
注意:在SQL中表示字符串,可以使用單引號也可以使用雙引號,他們兩個是等價關系,在SQL中沒有 " 字符類型 ",只有字符串類型,所以單引號就可以表示字符串。
在這里無論我們表的列數(shù)不匹配還是類型不匹配,都是會報錯的
mysql> insert into student values(2); ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> insert into student values ("zhangsan",3); ERROR 1366 (HY000): Incorrect integer value: 'zhangsan' for column 'id' at row 1
- 注意:出現(xiàn)ERROR意味著當前的操作是不生效的
拓展:
我們在這里還可以插入中文數(shù)據(jù):
mysql> insert into student values (2,"張三"); Query OK, 1 row affected (0.00 sec)
在這塊我們還需知道,數(shù)據(jù)庫表示中文需要明確字符編碼,MySQL默認的字符集叫做拉丁文,不支持中文,為了可以存儲,就需要把字符集改為UTF-8。在這里我們介紹一種一勞永逸的方法來修改字符集 --> 修改MySQL的配置文件
??如何修改MySQL配置:
1.先確認當前數(shù)據(jù)庫的字符集
show variables like ‘character%’;
mysql> show variables like 'character%'; +--------------------------+---------------------------------------------------------+ | Variable_name | Value | +--------------------------+---------------------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ | +--------------------------+---------------------------------------------------------+ 8 rows in set, 1 warning (0.00 sec)
可以看到我的數(shù)據(jù)庫就是UTF-8字符集
2.找到配置文件-my.ini
①:可以使用軟件Everything來尋找
搜索框里輸入my.ini即可找到,但是可能會出現(xiàn)多個my.ini導致無法辨別哪一個才是我們要找的,所以不推薦
②:在我們的系統(tǒng)找到MySQL并且完成這一系列操作
- 右鍵快捷鍵進入屬性:
- 拷貝出目標里面的內(nèi)容,這里就是MySQL的可執(zhí)行程序路徑和配置文件路徑
- 把MySQL配置文件的位置復制過來
這就是我們要找的配置文件
3.修改配置文件
①:修改配置文件之前,一定要先備份?。。?/span>復制粘貼到旁邊一份保存著,以免改錯還原不回去了!?。?/p>
②:編輯ini文件,用記事本打開即可,找到下面沒有#的地方,有#號的地方是注釋 1.ini文件中,有一些[],每個[]稱為是一個selection,相當于把一組功能有聯(lián)系的配置放到了一起,構成了一個selection。 2.具體在[mysql]加入的那句話,那個配置是按照鍵值對的方式來組織的,注意這里的鍵值對單詞拼寫,等于號兩邊不要有空格。 修改完成后記得保存(Ctrl + s)就可以退出了 4.配置文件不是修改完了就立即生效,還需要額外進行一些操作 ①:重啟MySQL服務器!不重啟就不會生效! 在我們的搜索里搜索服務,找到MySQL然后右鍵進行重啟即可 ②:修改配置文件,對已經(jīng)創(chuàng)建好的數(shù)據(jù)庫是沒有影響的,必須要刪除舊的數(shù)據(jù)庫,重建數(shù)據(jù)庫表。 至此MySQL配置修改就徹底結束了,繼續(xù)insert的探討 insert插入的時候可以指定列進行插入,不一定非得把這一行的所有列都插入數(shù)據(jù),可以想插入幾列就插入幾列 如上我們在學生名字這一列插入list,其他未被插入(id)填入的值就是默認值,默認的默認值就是啥都不填,也就是NULL。 insert語句還可以一次插入多條記錄,在values后面,帶有多組(),每個()之間使用 , 來分割 結語:插入是SQL中最簡單的一個操作,也是最常用的一個操作 查詢是SQL中最最重要也最復雜的操作,此處先介紹一下最簡單的查詢 其中*是通配符,表示匹配任意的列(所有的列) 注意理解這里的執(zhí)行過程,牢記,客戶端和服務器之間通過網(wǎng)絡進行通信 這一組結果是通過網(wǎng)絡返回的,最終呈現(xiàn)在客戶端上,這些數(shù)據(jù)是服務器篩選得到的數(shù)據(jù)結果,客戶端也是以表格的形式進行呈現(xiàn),但是大家不要把客戶端顯示的這個表格視為是服務器上數(shù)據(jù)的本體,這個客戶端上顯示的表格是個“臨時表”。 問題:如果當前數(shù)據(jù)庫的數(shù)據(jù)特別多,執(zhí)行上述select*會發(fā)生什么情況呢? 在執(zhí)行一些SQL的時候如果執(zhí)行的時間比較長,隨時可以按 Ctrl + c 來中斷,以免造成不必要的損失 當我們省略掉一些不必要的列的時候,就可以節(jié)省大量的磁盤IO和網(wǎng)絡帶寬了 MySQL是客戶端服務器結構的程序,在此處看到的這個表結果,也同樣是 " 臨時表 " 只是在客戶端這里顯示成這個樣子,而不是說服務器上就真有一個這樣的表,里面只存了id列。 select所有的操作結果都是臨時表,都不會影響到數(shù)據(jù)庫服務器原有的數(shù)據(jù)?。?! select 表達式 from 表名; decimal(3,1)表示的是三個數(shù)字長度,保留一位小時,如90.1,33.4 讓每個人的語文成績都加10分 再度查看表結構 如果數(shù)據(jù)變化超過了decimal(3,1),就是出現(xiàn)了多位數(shù)結果的情況,臨時表依舊會保證顯示的結果是正確的,但是嘗試往原始表中插入一個超出范圍的數(shù)據(jù)就是不行的?。?! 臨時表當中的列完全取決于select指定的列名 select 表達式 as 別名 from 表名; 如求語數(shù)英三科總分 如上我們看到的總分表達不是很合理,不直觀,我們可以對它起個別名 這樣我們的表達就清晰明了 還有一些奇奇怪怪的表達式查詢,如: 這樣的SQL語句也可以執(zhí)行,因為把10也當作是一個表達式(語法上沒錯,實際上沒啥意義) select distinct 列名 from 表名; 比如他們各自數(shù)學成績,有一個98.0重合的(上面數(shù)據(jù)沒有重合,此處假設),進行去重查詢之后就只剩下一個98.0 我們在上面繼續(xù)添加相同信息 對名字和數(shù)學列相同進行去重: select 列名 from 表名 order by 列名; 降序排序只需要在末尾加個 desc 即可,升序末尾是asc,但是升序是默認的,可以省略! 總成績降序排序 SQL中,如果拿 NULL 和其他類型進行混合運算,結果仍然是NULL 當指定多個列排序的時候,就相當于,先以第一個列為標準進行比較,如果第一列不分勝負,那么繼續(xù)按照第二列進行比較,以此類推… select 操作中,如果沒有使用 order by 那么查詢結果順序是不確定的,沒有具體的標準進行排序 select * from 表名 where 條件; 注: 進行條件查詢的時候,就是通過上述運算符組合最終完成的 ①:基本查詢 條件查詢,就是把表里的記錄,挨個往條件中帶入 where 中,別名并不能作為篩選條件 ②:and 與 or 加上括號之后就是先算括號里的 ③:范圍查詢 ④:模糊查詢:LIKE 通過 like 來完成模糊查詢,不一定完全相等,只要有一部分匹配即可 %孫–>匹配以孫結尾的數(shù)據(jù) 雖然數(shù)據(jù)庫支持模糊匹配,但是使用中也要慎重使用模糊匹配。模糊匹配本身,其實是非常低效的,如果做成正則表達式這樣效率就會更低。 ⑤:NULL的查詢 select 列名 from 表名 limit N offset M; select * 這樣的操作,容易把數(shù)據(jù)庫弄掛了,除了 select * 之外,只要你返回的記錄足夠多哪怕用了其他方式查詢,也是同樣有風險的,即使你加上 where 條件篩選,萬一篩選的結果很多,還是會弄壞服務器,最穩(wěn)妥的辦法就是加上 limit 。 update 表名 set 列名 = 值… where 子句 把孫悟空數(shù)學成績修改為80分 將曹孟德同學的數(shù)學成績變更為 60 分,語文成績變更為 70 分 將總成績倒數(shù)前三的 3 位同學的數(shù)學成績加上 30 分(先查明成績總單) 發(fā)現(xiàn)出現(xiàn)了錯誤,原因就是有數(shù)學加30超出了合理范圍,但是可以減去30(不能寫成 math += 30) update 后面的條件很重要,修改操作是針對條件篩選之后對剩下的數(shù)據(jù)進行的修改,如果沒寫條件,意味著就是對所以行都進行修改?。?! update 也是一種比較危險的操作,除了提前備份就基本無法還原改前數(shù)據(jù)?。?! delete from 表名 where 條件; delete from 表名; --> 把表里的記錄都刪除了,表只剩下一個空的表了 drop table 是把整個表都干掉了 以上就是MySQL表的增刪改查(CRUD)的詳細內(nèi)容,更多關于數(shù)據(jù)庫增刪改查的資料請關注腳本之家其它相關文章!mysql> insert into student (name) values ("lisi");
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values(1,"zhangsan"),(2,"lisi"),(3,"wangwu");
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
??三. 查詢(Retrieve)
??1. 全列查詢
mysql> select * from student;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 2 | 張三 |
| NULL | list |
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
+------+----------+
6 rows in set (0.00 sec)
??2. 指定列查詢
mysql> select id from student;
+------+
| id |
+------+
| 1 |
| 2 |
| NULL |
| 1 |
| 2 |
| 3 |
+------+
6 rows in set (0.00 sec)
??3. 查詢帶有表達式
mysql> create table exam_result (id int, name varchar(20), chinese decimal(3,1),math decimal(3,1), english decimal(3,1));
Query OK, 0 rows affected (0.01 sec)
mysql> desc exam_result;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| chinese | decimal(3,1) | YES | | NULL | |
| math | decimal(3,1) | YES | | NULL | |
| english | decimal(3,1) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> INSERT INTO exam_result (id,name, chinese, math, english) VALUES
-> (1,'唐三藏', 67, 98, 56),
-> (2,'孫悟空', 87.5, 78, 77),
-> (3,'豬悟能', 88, 98.5, 90),
-> (4,'曹孟德', 82, 84, 67),
-> (5,'劉玄德', 55.5, 85, 45),
-> (6,'孫權', 70, 73, 78.5),
-> (7,'宋公明', 75, 65, 30);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select * from exam_result;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
+------+-----------+---------+------+---------+
7 rows in set (0.00 sec)
mysql> select name,chinese + 10 from exam_result;
+-----------+--------------+
| name | chinese + 10 |
+-----------+--------------+
| 唐三藏 | 77.0 |
| 孫悟空 | 97.5 |
| 豬悟能 | 98.0 |
| 曹孟德 | 92.0 |
| 劉玄德 | 65.5 |
| 孫權 | 80.0 |
| 宋公明 | 85.0 |
+-----------+--------------+
7 rows in set (0.00 sec)
mysql> select * from exam_result;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
+------+-----------+---------+------+---------+
7 rows in set (0.00 sec)
??4. 起別名查詢
mysql> select name, chinese + math + english from exam_result;
+-----------+--------------------------+
| name | chinese + math + english |
+-----------+--------------------------+
| 唐三藏 | 221.0 |
| 孫悟空 | 242.5 |
| 豬悟能 | 276.5 |
| 曹孟德 | 233.0 |
| 劉玄德 | 185.5 |
| 孫權 | 221.5 |
| 宋公明 | 170.0 |
+-----------+--------------------------+
7 rows in set (0.00 sec)
mysql> select name, chinese + english + math as total from exam_result;
+-----------+-------+
| name | total |
+-----------+-------+
| 唐三藏 | 221.0 |
| 孫悟空 | 242.5 |
| 豬悟能 | 276.5 |
| 曹孟德 | 233.0 |
| 劉玄德 | 185.5 |
| 孫權 | 221.5 |
| 宋公明 | 170.0 |
+-----------+-------+
7 rows in set (0.00 sec)
mysql> select 10 from exam_result;
+----+
| 10 |
+----+
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
+----+
7 rows in set (0.00 sec)
??5. 去重查詢
mysql> select distinct math from exam_result;
+------+
| math |
+------+
| 98.0 |
| 78.0 |
| 84.0 |
| 85.0 |
| 73.0 |
| 65.0 |
+------+
6 rows in set (0.00 sec)
mysql> insert into exam_result (name, math) values ('唐三藏', 98.0);
Query OK, 1 row affected (0.00 sec)
mysql> select * from exam_result;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| NULL | 唐三藏 | NULL | 98.0 | NULL |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)
mysql> select distinct name, math from exam_result;
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98.0 |
| 孫悟空 | 78.0 |
| 豬悟能 | 98.5 |
| 曹孟德 | 84.0 |
| 劉玄德 | 85.0 |
| 孫權 | 73.0 |
| 宋公明 | 65.0 |
+-----------+------+
7 rows in set (0.00 sec)
??6. 排序查詢
mysql> select * from exam_result order by chinese;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| NULL | 唐三藏 | NULL | 98.0 | NULL |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)
mysql> select * from exam_result order by chinese desc;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| NULL | 唐三藏 | NULL | 98.0 | NULL |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)
mysql> select name, chinese + math + english as total from exam_result order by total desc;
+-----------+-------+
| name | total |
+-----------+-------+
| 豬悟能 | 276.5 |
| 孫悟空 | 242.5 |
| 曹孟德 | 233.0 |
| 孫權 | 221.5 |
| 唐三藏 | 221.0 |
| 劉玄德 | 185.5 |
| 宋公明 | 170.0 |
| 唐三藏 | NULL |
+-----------+-------+
8 rows in set (0.00 sec)
mysql> select * from exam_result order by math desc,chinese;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| NULL | 唐三藏 | NULL | 98.0 | NULL |
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)
??7. 條件查詢
運算符 說明 >,>=,<,<= 大于,大于等于,小于,小于等于 = 等于,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) mysql> select * from exam_result where english < 60;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
+------+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from exam_result where chinese > english;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
+------+-----------+---------+------+---------+
5 rows in set (0.00 sec)
mysql> select name, chinese + english + math as total from exam_result where chinese + math + english < 200;
+-----------+-------+
| name | total |
+-----------+-------+
| 劉玄德 | 185.5 |
| 宋公明 | 170.0 |
+-----------+-------+
2 rows in set (0.00 sec)
mysql> select name, chinese + english + math as total from exam_result where total < 200;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
mysql> select * from exam_result where chinese > 80 and english > 80;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
+------+-----------+---------+------+---------+
1 row in set (0.00 sec)
mysql> select * from exam_result where chinese > 80 or english > 80;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
+------+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from exam_result where chinese > 80 or english > 70 and math > 70;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
+------+-----------+---------+------+---------+
4 rows in set (0.00 sec)
mysql> select * from exam_result where chinese >= 80 and chinese <= 90;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
+------+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from exam_result where chinese between 80 and 90;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
+------+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from exam_result where math in (58,59,98,99);
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| NULL | bit me | NULL | 98.0 | NULL |
+------+-----------+---------+------+---------+
2 rows in set (0.00 sec)
mysql> select * from exam_result where math = 58 or math = 59 or math = 98 or math = 99;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| NULL | bit me | NULL | 98.0 | NULL |
+------+-----------+---------+------+---------+
2 rows in set (0.00 sec)
mysql> select * from exam_result where name like '孫%';
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
+------+-----------+---------+------+---------+
2 rows in set (0.00 sec)
%孫%–>匹配含孫的數(shù)據(jù)
%–>匹配任意數(shù)據(jù)mysql> select * from exam_result;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| NULL | 唐三藏 | NULL | 98.0 | NULL |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)
mysql> select * from exam_result where chinese = NULL;
Empty set (0.00 sec)
mysql> select * from exam_result where chinese <=> NULL;
+------+--------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------+---------+------+---------+
| NULL | 唐三藏 | NULL | 98.0 | NULL |
+------+--------+---------+------+---------+
1 row in set (0.00 sec)
mysql> select * from exam_result where chinese is NULL;
+------+--------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------+---------+------+---------+
| NULL | 唐三藏 | NULL | 98.0 | NULL |
+------+--------+---------+------+---------+
1 row in set (0.00 sec)
??8. 分頁查詢
select 列名 from 表名 limit M, Nmysql> select * from exam_result;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| NULL | 唐三藏 | NULL | 98.0 | NULL |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)
mysql> select * from exam_result limit 3;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
+------+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from exam_result limit 3 offset 3;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
+------+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from exam_result limit 3 offset 6;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| NULL | 唐三藏 | NULL | 98.0 | NULL |
+------+-----------+---------+------+---------+
2 rows in set (0.00 sec)
mysql> select name, chinese + english + math as total from exam_result order by total desc limit 3;
+-----------+-------+
| name | total |
+-----------+-------+
| 豬悟能 | 276.5 |
| 孫悟空 | 242.5 |
| 曹孟德 | 233.0 |
+-----------+-------+
3 rows in set (0.00 sec)
??四. 修改(Update)
mysql> update exam_result set math = 80 where name = '孫悟空';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from exam_result;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 80.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| NULL | 唐三藏 | NULL | 98.0 | NULL |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)
mysql> update exam_result set chinese = 70, math = 60 where name = '曹孟德';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from exam_result;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 80.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 70.0 | 60.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| NULL | 唐三藏 | NULL | 98.0 | NULL |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)
mysql> select name, chinese + english + math as total from exam_result order by total;
+-----------+-------+
| name | total |
+-----------+-------+
| 唐三藏 | NULL |
| 宋公明 | 170.0 |
| 劉玄德 | 185.5 |
| 曹孟德 | 197.0 |
| 唐三藏 | 221.0 |
| 孫權 | 221.5 |
| 孫悟空 | 244.5 |
| 豬悟能 | 276.5 |
+-----------+-------+
8 rows in set (0.00 sec)
mysql> update exam_result set math = math + 30 order by chinese + english + math limit 3;
ERROR 1264 (22003): Out of range value for column 'math' at row 1
??五. 刪除(Delete)
mysql> delete from exam_result where name = '唐三藏';
Query OK, 1 row affected (0.00 sec)
mysql> select * from exam_result;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 2 | 孫悟空 | 87.5 | 80.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 70.0 | 60.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
+------+-----------+---------+------+---------+
6 rows in set (0.00 sec)
mysql> delete from exam_result;
Query OK, 7 rows affected (0.00 sec)
mysql> select * from exam_result;
Empty set (0.00 sec)
相關文章
windows下MySQL數(shù)據(jù)庫移動到其它盤
大家好,本篇文章主要講的是windows下MySQL數(shù)據(jù)庫移動到其它盤,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏2021-12-12MySQL觸發(fā)器之判斷更新操作前后數(shù)據(jù)是否改變
這篇文章主要介紹了MySQL觸發(fā)器之判斷更新操作前后數(shù)據(jù)是否改變方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08MySQL數(shù)據(jù)時區(qū)問題以及datetime和timestamp類型存儲的差異
這篇文章主要介紹了MySQL數(shù)據(jù)時區(qū)問題以及datetime和timestamp類型存儲的差異,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11mysql外鍵(Foreign Key)介紹和創(chuàng)建外鍵的方法
這篇文章主要介紹了mysql外鍵(Foreign Key)命令和添加外鍵方法,需要的朋友可以參考下2014-02-02