MySQL表的增刪改查(CRUD)
??一. CRUD
- CRUD : Create,Retrieve,Update,Delete
- 新增數(shù)據(jù)
- 查詢數(shù)據(jù)
- 修改數(shù)據(jù)
- 刪除數(shù)據(jù)
- MySQL的工作就是組織管理數(shù)據(jù),先保存,保存好了后好進(jìn)行增刪改查
增刪改查的前提是已經(jīng)把數(shù)據(jù)庫(kù)創(chuàng)建好,并且選中了,表也創(chuàng)建就緒
- 注釋:在SQL中可以使用“–空格+描述”來(lái)表示注釋說(shuō)明
- CRUD 即增加(Create)、查詢(Retrieve)、更新(Update)、刪除(Delete)四個(gè)單詞的首字母縮寫
??二. 新增(Create)
insert into 表名 values(值,值,值…);
- 注意此處的值的個(gè)數(shù)要和表的列數(shù)匹配,值的類型也要和列的類型匹配(不匹配就會(huì)報(bào)錯(cuò)?。。。?/span>
所以也更好的體現(xiàn)出關(guān)系型數(shù)據(jù)庫(kù)的一個(gè)優(yōu)勢(shì):對(duì)數(shù)據(jù)進(jìn)行更嚴(yán)格的校驗(yàn)檢查,更容易發(fā)現(xiàn)問(wèn)題!
1.我們先在庫(kù)里創(chuàng)建一個(gè)學(xué)生表:
mysql> create table student(id int, name varchar(20)); Query OK, 0 rows affected (0.01 sec)
2.查看表的結(jié)構(gòu)
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中表示字符串,可以使用單引號(hào)也可以使用雙引號(hào),他們兩個(gè)是等價(jià)關(guān)系,在SQL中沒(méi)有 " 字符類型 ",只有字符串類型,所以單引號(hào)就可以表示字符串。
在這里無(wú)論我們表的列數(shù)不匹配還是類型不匹配,都是會(huì)報(bào)錯(cuò)的
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意味著當(dāng)前的操作是不生效的
拓展:
我們?cè)谶@里還可以插入中文數(shù)據(jù):
mysql> insert into student values (2,"張三"); Query OK, 1 row affected (0.00 sec)
在這塊我們還需知道,數(shù)據(jù)庫(kù)表示中文需要明確字符編碼,MySQL默認(rèn)的字符集叫做拉丁文,不支持中文,為了可以存儲(chǔ),就需要把字符集改為UTF-8。在這里我們介紹一種一勞永逸的方法來(lái)修改字符集 --> 修改MySQL的配置文件
??如何修改MySQL配置:
1.先確認(rèn)當(dāng)前數(shù)據(jù)庫(kù)的字符集
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ù)庫(kù)就是UTF-8字符集
2.找到配置文件-my.ini
①:可以使用軟件Everything來(lái)尋找
搜索框里輸入my.ini即可找到,但是可能會(huì)出現(xiàn)多個(gè)my.ini導(dǎo)致無(wú)法辨別哪一個(gè)才是我們要找的,所以不推薦
②:在我們的系統(tǒng)找到MySQL并且完成這一系列操作
- 右鍵快捷鍵進(jìn)入屬性:
- 拷貝出目標(biāo)里面的內(nèi)容,這里就是MySQL的可執(zhí)行程序路徑和配置文件路徑
- 把MySQL配置文件的位置復(fù)制過(guò)來(lái)
這就是我們要找的配置文件
3.修改配置文件
①:修改配置文件之前,一定要先備份?。。?/span>復(fù)制粘貼到旁邊一份保存著,以免改錯(cuò)還原不回去了?。。?/p>
②:編輯ini文件,用記事本打開(kāi)即可,找到下面沒(méi)有#的地方,有#號(hào)的地方是注釋 1.ini文件中,有一些[],每個(gè)[]稱為是一個(gè)selection,相當(dāng)于把一組功能有聯(lián)系的配置放到了一起,構(gòu)成了一個(gè)selection。 2.具體在[mysql]加入的那句話,那個(gè)配置是按照鍵值對(duì)的方式來(lái)組織的,注意這里的鍵值對(duì)單詞拼寫,等于號(hào)兩邊不要有空格。 修改完成后記得保存(Ctrl + s)就可以退出了 4.配置文件不是修改完了就立即生效,還需要額外進(jìn)行一些操作 ①:重啟MySQL服務(wù)器!不重啟就不會(huì)生效! 在我們的搜索里搜索服務(wù),找到MySQL然后右鍵進(jìn)行重啟即可 ②:修改配置文件,對(duì)已經(jīng)創(chuàng)建好的數(shù)據(jù)庫(kù)是沒(méi)有影響的,必須要?jiǎng)h除舊的數(shù)據(jù)庫(kù),重建數(shù)據(jù)庫(kù)表。 至此MySQL配置修改就徹底結(jié)束了,繼續(xù)insert的探討 insert插入的時(shí)候可以指定列進(jìn)行插入,不一定非得把這一行的所有列都插入數(shù)據(jù),可以想插入幾列就插入幾列 如上我們?cè)趯W(xué)生名字這一列插入list,其他未被插入(id)填入的值就是默認(rèn)值,默認(rèn)的默認(rèn)值就是啥都不填,也就是NULL。 insert語(yǔ)句還可以一次插入多條記錄,在values后面,帶有多組(),每個(gè)()之間使用 , 來(lái)分割 結(jié)語(yǔ):插入是SQL中最簡(jiǎn)單的一個(gè)操作,也是最常用的一個(gè)操作 查詢是SQL中最最重要也最復(fù)雜的操作,此處先介紹一下最簡(jiǎn)單的查詢 其中*是通配符,表示匹配任意的列(所有的列) 注意理解這里的執(zhí)行過(guò)程,牢記,客戶端和服務(wù)器之間通過(guò)網(wǎng)絡(luò)進(jìn)行通信 這一組結(jié)果是通過(guò)網(wǎng)絡(luò)返回的,最終呈現(xiàn)在客戶端上,這些數(shù)據(jù)是服務(wù)器篩選得到的數(shù)據(jù)結(jié)果,客戶端也是以表格的形式進(jìn)行呈現(xiàn),但是大家不要把客戶端顯示的這個(gè)表格視為是服務(wù)器上數(shù)據(jù)的本體,這個(gè)客戶端上顯示的表格是個(gè)“臨時(shí)表”。 問(wèn)題:如果當(dāng)前數(shù)據(jù)庫(kù)的數(shù)據(jù)特別多,執(zhí)行上述select*會(huì)發(fā)生什么情況呢? 在執(zhí)行一些SQL的時(shí)候如果執(zhí)行的時(shí)間比較長(zhǎng),隨時(shí)可以按 Ctrl + c 來(lái)中斷,以免造成不必要的損失 當(dāng)我們省略掉一些不必要的列的時(shí)候,就可以節(jié)省大量的磁盤IO和網(wǎng)絡(luò)帶寬了 MySQL是客戶端服務(wù)器結(jié)構(gòu)的程序,在此處看到的這個(gè)表結(jié)果,也同樣是 " 臨時(shí)表 " 只是在客戶端這里顯示成這個(gè)樣子,而不是說(shuō)服務(wù)器上就真有一個(gè)這樣的表,里面只存了id列。 select所有的操作結(jié)果都是臨時(shí)表,都不會(huì)影響到數(shù)據(jù)庫(kù)服務(wù)器原有的數(shù)據(jù)?。?! select 表達(dá)式 from 表名; decimal(3,1)表示的是三個(gè)數(shù)字長(zhǎng)度,保留一位小時(shí),如90.1,33.4 讓每個(gè)人的語(yǔ)文成績(jī)都加10分 再度查看表結(jié)構(gòu) 如果數(shù)據(jù)變化超過(guò)了decimal(3,1),就是出現(xiàn)了多位數(shù)結(jié)果的情況,臨時(shí)表依舊會(huì)保證顯示的結(jié)果是正確的,但是嘗試往原始表中插入一個(gè)超出范圍的數(shù)據(jù)就是不行的!??! 臨時(shí)表當(dāng)中的列完全取決于select指定的列名 select 表達(dá)式 as 別名 from 表名; 如求語(yǔ)數(shù)英三科總分 如上我們看到的總分表達(dá)不是很合理,不直觀,我們可以對(duì)它起個(gè)別名 這樣我們的表達(dá)就清晰明了 還有一些奇奇怪怪的表達(dá)式查詢,如: 這樣的SQL語(yǔ)句也可以執(zhí)行,因?yàn)榘?0也當(dāng)作是一個(gè)表達(dá)式(語(yǔ)法上沒(méi)錯(cuò),實(shí)際上沒(méi)啥意義) select distinct 列名 from 表名; 比如他們各自數(shù)學(xué)成績(jī),有一個(gè)98.0重合的(上面數(shù)據(jù)沒(méi)有重合,此處假設(shè)),進(jìn)行去重查詢之后就只剩下一個(gè)98.0 我們?cè)谏厦胬^續(xù)添加相同信息 對(duì)名字和數(shù)學(xué)列相同進(jìn)行去重: select 列名 from 表名 order by 列名; 降序排序只需要在末尾加個(gè) desc 即可,升序末尾是asc,但是升序是默認(rèn)的,可以省略! 總成績(jī)降序排序 SQL中,如果拿 NULL 和其他類型進(jìn)行混合運(yùn)算,結(jié)果仍然是NULL 當(dāng)指定多個(gè)列排序的時(shí)候,就相當(dāng)于,先以第一個(gè)列為標(biāo)準(zhǔn)進(jìn)行比較,如果第一列不分勝負(fù),那么繼續(xù)按照第二列進(jìn)行比較,以此類推… select 操作中,如果沒(méi)有使用 order by 那么查詢結(jié)果順序是不確定的,沒(méi)有具體的標(biāo)準(zhǔn)進(jìn)行排序 select * from 表名 where 條件; 注: 進(jìn)行條件查詢的時(shí)候,就是通過(guò)上述運(yùn)算符組合最終完成的 ①:基本查詢 條件查詢,就是把表里的記錄,挨個(gè)往條件中帶入 where 中,別名并不能作為篩選條件 ②:and 與 or 加上括號(hào)之后就是先算括號(hào)里的 ③:范圍查詢 ④:模糊查詢:LIKE 通過(guò) like 來(lái)完成模糊查詢,不一定完全相等,只要有一部分匹配即可 %孫–>匹配以孫結(jié)尾的數(shù)據(jù) 雖然數(shù)據(jù)庫(kù)支持模糊匹配,但是使用中也要慎重使用模糊匹配。模糊匹配本身,其實(shí)是非常低效的,如果做成正則表達(dá)式這樣效率就會(huì)更低。 ⑤:NULL的查詢 select 列名 from 表名 limit N offset M; select * 這樣的操作,容易把數(shù)據(jù)庫(kù)弄掛了,除了 select * 之外,只要你返回的記錄足夠多哪怕用了其他方式查詢,也是同樣有風(fēng)險(xiǎn)的,即使你加上 where 條件篩選,萬(wàn)一篩選的結(jié)果很多,還是會(huì)弄壞服務(wù)器,最穩(wěn)妥的辦法就是加上 limit 。 update 表名 set 列名 = 值… where 子句 把孫悟空數(shù)學(xué)成績(jī)修改為80分 將曹孟德同學(xué)的數(shù)學(xué)成績(jī)變更為 60 分,語(yǔ)文成績(jī)變更為 70 分 將總成績(jī)倒數(shù)前三的 3 位同學(xué)的數(shù)學(xué)成績(jī)加上 30 分(先查明成績(jī)總單) 發(fā)現(xiàn)出現(xiàn)了錯(cuò)誤,原因就是有數(shù)學(xué)加30超出了合理范圍,但是可以減去30(不能寫成 math += 30) update 后面的條件很重要,修改操作是針對(duì)條件篩選之后對(duì)剩下的數(shù)據(jù)進(jìn)行的修改,如果沒(méi)寫條件,意味著就是對(duì)所以行都進(jìn)行修改?。?! update 也是一種比較危險(xiǎn)的操作,除了提前備份就基本無(wú)法還原改前數(shù)據(jù)?。?! delete from 表名 where 條件; delete from 表名; --> 把表里的記錄都刪除了,表只剩下一個(gè)空的表了 drop table 是把整個(gè)表都干掉了 以上就是MySQL表的增刪改查(CRUD)的詳細(xì)內(nèi)容,更多關(guān)于數(shù)據(jù)庫(kù)增刪改查的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!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. 查詢帶有表達(dá)式
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,'孫權(quán)', 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 | 孫權(quán) | 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 |
| 孫權(quán) | 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 | 孫權(quán) | 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 |
| 孫權(quán) | 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 |
| 孫權(quán) | 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 | 孫權(quán) | 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 |
| 孫權(quán) | 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 | 孫權(quán) | 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 | 孫權(quán) | 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 |
| 孫權(quán) | 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 | 孫權(quán) | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)
??7. 條件查詢
運(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)算符 說(shuō)明 AND 多個(gè)條件必須都為 TRUE(1),結(jié)果才是 TRUE(1) OR 任意一個(gè)條件為 TRUE(1), 結(jié)果為 TRUE(1) NOT 條件為 TRUE(1),結(jié)果為 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 | 孫權(quán) | 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 | 孫權(quán) | 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 | 孫權(quán) | 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. 分頁(yè)查詢
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 | 孫權(quán) | 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 | 孫權(quán) | 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 | 孫權(quán) | 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 | 孫權(quán) | 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 |
| 孫權(quán) | 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 | 孫權(quán) | 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)
相關(guān)文章
一臺(tái)linux主機(jī)啟動(dòng)多個(gè)MySQL數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了一臺(tái)linux主機(jī)啟動(dòng)多個(gè)MySQL數(shù)據(jù)庫(kù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03mysql5.7.19 解壓版安裝教程詳解(附送純凈破解中文版SQLYog)
Mysql5.7.19版本是今年新推出的版本,最近幾個(gè)版本的MySQL都不再是安裝版,都是解壓版了,大家在使用過(guò)程中遇到很多問(wèn)題,下面小編給大家?guī)?lái)了MySQL5.7.19 解壓版安裝教程詳解,感興趣的朋友一起看看吧2017-10-10出現(xiàn)錯(cuò)誤mysql Table ''performance_schema...解決辦法
這篇文章主要介紹了解決出現(xiàn)錯(cuò)誤mysql Table 'performance_schema.session_variables' doesn't exist的相關(guān)資料,需要的朋友可以參考下2017-04-04MySQL入門(二) 數(shù)據(jù)庫(kù)數(shù)據(jù)類型詳解
這個(gè)數(shù)據(jù)庫(kù)所遇到的數(shù)據(jù)類型今天統(tǒng)統(tǒng)在這里講清楚了,以后在看到什么數(shù)據(jù)類型,咱度應(yīng)該認(rèn)識(shí),對(duì)我來(lái)說(shuō),最不熟悉的應(yīng)該就是時(shí)間類型這塊了。但是通過(guò)今天的學(xué)習(xí),已經(jīng)解惑了。下面就跟著我的節(jié)奏去把這個(gè)拿下吧2018-07-07MySQL數(shù)據(jù)庫(kù)遠(yuǎn)程訪問(wèn)權(quán)限如何打開(kāi)(兩種方法)
本文通過(guò)兩種方法給大家介紹MySQL數(shù)據(jù)庫(kù)遠(yuǎn)程訪問(wèn)權(quán)限的打開(kāi)方法,非常不錯(cuò),實(shí)用性非常高,感興趣的朋友一起看看吧2016-05-05