佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > MySQL表的增刪改查基礎(chǔ)教程

MySQL表的增刪改查基礎(chǔ)教程

熱門標(biāo)簽:百度競價(jià)排名 Linux服務(wù)器 網(wǎng)站排名優(yōu)化 呼叫中心市場需求 鐵路電話系統(tǒng) AI電銷 地方門戶網(wǎng)站 服務(wù)外包

1. 新增(Create)

insert into [表名] (字段1, 字段2,....) value (value1, value2, ...);

insert into [表名] (字段1, 字段2, ....) values
(value1, ...),
(value2, ...),
(value3, ...);

實(shí)例:

創(chuàng)建一個(gè)學(xué)生成績表

CREATE TABLE exam_result (
 id INT,
 name VARCHAR(20),
 chinese DECIMAL(3,1),
 math DECIMAL(3,1),
 english DECIMAL(3,1)
);

1.1 單行數(shù)據(jù) + 全列插入

-- 插入兩條記錄,value_list 數(shù)量必須和定義表的列的數(shù)量及順序一致
insert into exam_result value ( 1, 'tom', 68, 98, 56); 
insert into exam_result value ( 2, 'jum', 87.5, 78, 77); 

每次插入數(shù)據(jù), 為一條記錄, 包含了若干個(gè)列~~

列的數(shù)目和數(shù)據(jù)類型要和表的結(jié)構(gòu)對應(yīng)~

value 前省略指定列默認(rèn)為全列插入

1.2 多行數(shù)據(jù) + 指定列

- 插入兩條記錄,value_list 數(shù)量必須和指定列數(shù)量及順序一致
INSERT INTO exam_result (id,name, chinese, math, english) VALUES
 (1,'tom', 67, 98, 56),
 (2,'jum', 87.5, 78, 77),
 (3,'lim', 88, 98.5, 90),
 (4,'tim', 82, 84, 67),
 (5,'huy', 55.5, 85, 45),
 (6,'sun', 70, 73, 78.5),
 (7,'ming', 75, 65, 30);

2. 查詢(Retrieve)

2.1 全列查詢

select * from [表名];

*表示通配符, 意思就是查找所有的列

2.2 指定列查詢

select [指定查詢列] from [表名];

2.3 查詢字段為表達(dá)式

select [字段表達(dá)式] from [表名];

2.4 別名

select colum [as] [列名] from [表名];

2.5 去重: DISTINCT

使用 distinct 關(guān)鍵字對某列數(shù)據(jù)去重

--98 分重復(fù)了
select math from exam_result;
+------+
| math |
+------+
| 98.0 |
| 78.0 |
| 98.0 |
| 84.0 |
| 85.0 |
| 73.0 |
| 65.0 |
+------+

-- 去重結(jié)果
select distinct math from exam_result;
+------+
| math |
+------+
| 98.0 |
| 78.0 |
| 84.0 |
| 85.0 |
| 73.0 |
| 65.0 |
+------+

2.6 排序: ORDER BY

select * from [表名] order by [排序字段];

用 order by 指定某一列進(jìn)行排序, 默認(rèn)按照升序排序.

顯式加上 desc , 就是降序排序. 使用 asc 也是升序

select name, math from exam_result order by math desc;
+------+------+
| name | math |
+------+------+
| tom | 98.0 |
| lim | 98.0 |
| huy | 85.0 |
| tim | 84.0 |
| jum | 78.0 |
| sun | 73.0 |
| ming | 65.0 |
+------+------+

NULL 數(shù)據(jù)排序,視為比任何值都小,升序出現(xiàn)在最上面,降序出現(xiàn)在最下面

排序也可以指定多個(gè)列執(zhí)行

select * from exam_result order by math desc, chinese desc;
+------+------+---------+------+---------+
| id | name | chinese | math | english |
+------+------+---------+------+---------+
| 3 | lim | 88.0 | 98.0 | 90.0 |
| 1 | tom | 67.0 | 98.0 | 56.0 |
| 5 | huy | 55.5 | 85.0 | 45.0 |
| 4 | tim | 82.0 | 84.0 | 67.0 |
| 2 | jum | 87.5 | 78.0 | 77.0 |
| 6 | sun | 70.0 | 73.0 | 78.5 |
| 7 | ming | 75.0 | 65.0 | 30.0 |
+------+------+---------+------+---------+

多列排序時(shí), 是在第一列區(qū)分不出來大小的時(shí)候, 再按第二列排序.

2.7 條件查詢: WHERE

比較運(yùn)算符

運(yùn)算符 說明
>, >=, , = 大于,大于等于,小于,小于等于
= 等于,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)算符 說明
AND 多個(gè)條件必須都為 TRUE(1),結(jié)果才是 TRUE(1)
OR 任意一個(gè)條件為 TRUE(1), 結(jié)果為 TRUE(1)
NOT 條件為 TRUE(1),結(jié)果為 FALSE(0)

注意:

  • 列的別名不能再 where 中使用~~
  • AND的優(yōu)先級高于OR,在同時(shí)使用時(shí),需要使用小括號()包裹優(yōu)先執(zhí)行的部分

實(shí)例:

基本查詢:

-- 查詢英語不及格的同學(xué)及英語成績 (  60 )
select name, english from exam_result where english  60;

-- 查詢語文成績好于英語成績的同學(xué)
select name, chinese, english from exam_result where chinese > english;

-- 查詢總分在 200 分以下的同學(xué)
select name, chinese + math + english as total from exam_result where chinese + math + english  200;

AND 與 OR:

-- 查詢語文成績大于80分,且英語成績大于80分的同學(xué)
select * from exam_result where chinese > 80 and english > 80;

-- 查詢語文成績大于80分,或英語成績大于80分的同學(xué)
select * from exam_result where chinese > 80 or english > 80;

關(guān)于優(yōu)先級問題, and 比 or 更優(yōu)先,

范圍查詢:

1.BETWEEN … AND …

-- 查詢語文成績在 [80, 90] 分的同學(xué)及語文成績
select name, chinese from exam_result where chinese BETWEEN 80 AND 90;

select name, chinese, from exam_result where chinese >= 80 and chinese = 90;

IN

 -- 查詢數(shù)學(xué)成績是 58 或者 59 或者 98 或者 99 分的同學(xué)及數(shù)學(xué)成績
select name, math from exam_result where math in (58, 59, 98, 99);

模糊查詢: LIKE

select name from exam_result where name like 't%';
+------+
| name |
+------+
| tom |
| tim |
+------+

% 是一個(gè)通配符, 可以用來代替任意多個(gè)字符

t% 找出以 t 開頭的字符串

%t 找出以 t 結(jié)尾的字符串

%t% 找出包含 t 的

除了 % 之外, 還有 _ ,(_ 只能代表一個(gè)字符~)

select name from exam_result where name like 't__';
+------+
| name |
+------+
| tom |
| tim |
+------+

通配符也能針對數(shù)字進(jìn)行模糊查詢

select name, chinese from exam_result where chinese like '%8%';
+------+---------+
| name | chinese |
+------+---------+
| jum |  87.5 |
| lim |  88.0 |
| tim |  82.0 |
+------+---------+

注意:

模糊查詢看起來比較好用, 實(shí)際執(zhí)行效率低下

NULL 的查詢: IS [NOT] NULL

 select name from exam_result where id id not null;

2.8 分頁查詢: LIMIT

-- 最初數(shù)據(jù)表
select * from exam_result;
+------+------+---------+------+---------+
| id  | name | chinese | math | english |
+------+------+---------+------+---------+
|  1 | tom |  67.0 | 98.0 |  56.0 |
|  2 | jum |  87.5 | 78.0 |  77.0 |
|  3 | lim |  88.0 | 98.0 |  90.0 |
|  4 | tim |  82.0 | 84.0 |  67.0 |
|  5 | huy |  55.5 | 85.0 |  45.0 |
|  6 | sun |  70.0 | 73.0 |  78.5 |
|  7 | ming |  75.0 | 65.0 |  30.0 |
+------+------+---------+------+---------+

-- 前三條記錄
select * from exam_result limit 3;
+------+------+---------+------+---------+
| id  | name | chinese | math | english |
+------+------+---------+------+---------+
|  1 | tom |  67.0 | 98.0 |  56.0 |
|  2 | jum |  87.5 | 78.0 |  77.0 |
|  3 | lim |  88.0 | 98.0 |  90.0 |
+------+------+---------+------+---------+

-- 從第三條開始的三條記錄
select * from exam_result limit 3 offset 3;
+------+------+---------+------+---------+
| id  | name | chinese | math | english |
+------+------+---------+------+---------+
|  4 | tim |  82.0 | 84.0 |  67.0 |
|  5 | huy |  55.5 | 85.0 |  45.0 |
|  6 | sun |  70.0 | 73.0 |  78.5 |
+------+------+---------+------+---------+

offset 表示從第幾條開始查找, offset 可以省略

select * from exam_result limit 3 , 4;
+------+------+---------+------+---------+
| id  | name | chinese | math | english |
+------+------+---------+------+---------+
|  4 | tim |  82.0 | 84.0 |  67.0 |
|  5 | huy |  55.5 | 85.0 |  45.0 |
|  6 | sun |  70.0 | 73.0 |  78.5 |
|  7 | ming |  75.0 | 65.0 |  30.0 |
+------+------+---------+------+---------+

3. 修改(Update)

– 將總成績倒數(shù)前三的 3 位同學(xué)的數(shù)學(xué)成績加上 30 分

update exam_result set math = math + 30 order by chinese + math + english limit 3;

update 不加條件, 就可以針對所有

4. 刪除(Delete)

delete from [表名];
-- 刪除 ming 同學(xué)的考試成績
delete from exam_result where name = 'ming';

-- 刪除整張表
delete from exam_result;

如果不指定條件, 此時(shí)就把整個(gè)表刪除掉了, (與 drop 刪除表還有不同)

delete 刪除后表為 null, drop 刪除后表就不存在了

5. 常用 新增

-- 單行插入
insert into [表名] (字段1, ..., 字段N) values (value1, ...,value N);
-- 多行插入
insert into [表名](字段1, ..., 字段N) values
(value1, ...),
(value2, ...),
(value3, ...);

查詢

--全表查詢
select * from [表名];
--指定列查詢
select [列名1, 列名2,...] from [表名];
--查詢表達(dá)式字段
select [表達(dá)式1, 表達(dá)式2,...] from [表名];
--別名
select 
--去重 DISTINCT
select distinct [字段] from [表名];
-- 排序ORDER BY
select * from [表名] order by [排序字段];
-- 條件查詢WHERE
-- (1)比較運(yùn)算符 (2)BETWEEN ... AND ... (3)IN (4)IS NULL (5)LIKE (6)AND (7)OR 
(8)NOT
select * from [表名] where [條件];

修改

update [表] set [修改內(nèi)容1, 修改內(nèi)容2, ....] where [條件];

刪除

delete from [表名] where [條件];

總結(jié)

到此這篇關(guān)于MySQL表增刪改查的文章就介紹到這了,更多相關(guān)MySQL表增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • mysql增刪改查基礎(chǔ)語句
  • Mysql的增刪改查語句簡單實(shí)現(xiàn)
  • mysql觸發(fā)器之觸發(fā)器的增刪改查操作示例
  • Mysql表,列,庫增刪改查問題小結(jié)
  • MySQL 詳細(xì)單表增刪改查crud語句
  • mysql視圖之管理視圖實(shí)例詳解【增刪改查操作】

標(biāo)簽:衡水 銅川 蘭州 湘潭 湖南 黃山 崇左 仙桃

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL表的增刪改查基礎(chǔ)教程》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    东乡| 比如县| 云浮市| 云南省| 清水河县| 葫芦岛市| 乌海市| 于田县| 平谷区| 松滋市| 九龙县| 庆安县| 武威市| 兴海县| 固镇县| 若尔盖县| 家居| 芜湖县| 自治县| 怀仁县| 绩溪县| 垫江县| 大同县| 二连浩特市| 乡城县| 河池市| 德阳市| 新宁县| 泸州市| 枣庄市| 紫云| 黔西县| 原阳县| 金乡县| 邹平县| 耿马| 漳浦县| 天气| 乡宁县| 蓝山县| 巨鹿县|