MySQL表的增刪改查及查詢表中數(shù)據(jù)的三種方式(入門必掌握)
一.操作表中的數(shù)據(jù)(DML)
數(shù)據(jù)準備
--創(chuàng)建一個student表
表中字段:
學號, id int
姓名, name varchar(20)
年齡, age int
性別, sex char(1)
地址, address varchar(40)
# 創(chuàng)建學生表
CREATE TABLE student(
id INT,
name VARCHAR(20),
age INT,
sex CHAR(1),
address VARCHAR(40)
);1.新增數(shù)據(jù)
1.1 單行數(shù)據(jù)+全列插入
語法格式:insert to 表名 values(對應的數(shù)據(jù)庫字段);
#插入兩條記錄,value_list 數(shù)量必須和定義表的列的數(shù)量及順序一致 insert to student values(1,'孫悟空',20,'男','花果山'); insert to student values(2,'豬八戒',18,'男','云棧洞');
1.2 多行數(shù)據(jù)+指定列插入
語法格式:insert to 表名 (指定的數(shù)據(jù)庫字段列)values(與指定的數(shù)據(jù)庫字段列相對應)
#插入兩條記錄,value_list 數(shù)量必須和指定列數(shù)量及順序一致 insert to student (id,name,age,sex,address) values (3,'沙悟凈',18,'男','流沙河'), (4,'白龍馬',16,'男','東海龍宮'), (5,'哪吒',17,'女','天宮');
注意:
① 值與字段必須要對應,個數(shù)相同&數(shù)據(jù)類型相同
②值的數(shù)據(jù)大小,必須在字段指定的長度范圍內(nèi) insert into 表名 (字段名1,字段名2...) values(字段值1,字段值2...); 。
③varchar char date類型的值必須使用單引號,或者雙引號 包裹。
④如果要插入空值,可以忽略不寫,或者插入null 。
⑤如果插入指定字段的值,必須要上寫列名。
2.更新數(shù)據(jù)
認識一個關(guān)鍵字where ,這個關(guān)鍵字在mysql語句中稱為條件查詢語句(在查詢的時候會細講)
語法格式:不帶條件的修改
update 表名 set 列名 = 值;(慎用)
語法格式:帶條件的修改
update 表名 set 列名 = 值 where 字段名 = 值;(字段名為定義的字段,值是保存的字段下的數(shù)據(jù))
2.1 將所有性別改為女(慎用)
update student set sex = '女'
2.2 將id為2的學生性別改為男
update student set sex = '男' where id = 2;
2.3 將id為2的學生的年齡改為(20),地址改為天宮
update student set age = 20,address = '天宮' where id = 2;
3.刪除數(shù)據(jù)
語法格式1: 刪除所有數(shù)據(jù)
delete from 表名;
語法格式2: 指定條件刪除數(shù)據(jù)
delete from 表名 where 字段名 = 值;
3.1 刪除id為1的數(shù)據(jù)
delete from student where id = 1;
3.2 刪除所有數(shù)據(jù)
delete from student;
二.查詢表中的數(shù)據(jù)(DQL)
數(shù)據(jù)準備
創(chuàng)建考試成績表
創(chuàng)建考試成績表 DROP TABLE IF EXISTS exam_result; CREATE TABLE exam_result ( id INT, name VARCHAR(20), chinese DECIMAL(3,1), math DECIMAL(3,1), english DECIMAL(3,1) ); -- 插入測試數(shù)據(jù) 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);
查詢操作不會對數(shù)據(jù)庫中的內(nèi)容進行修改,只是顯示數(shù)據(jù)庫的內(nèi)容
1.簡單查詢
1.1 全列查詢
查詢成績表的所有信息
語法格式 :select 列名 from 表名;
# *代表所有字段 select * from exam_result;
1.2 指定列查詢
查詢id,name的學生
語法格式:select 字段1,字段2 from 表名;
#字段不用按照指定順序書寫 select id,name from exam_result;
1.3 查詢字段為表達式
計算每個學生的總成績,且只顯示 id,name,和總分數(shù)
select id,name, chinese + math + english from exam_result;
1.4 別名查詢
將所有學生的信息查詢出來,并將列名改為中文
別名查詢就是給字段起外號,比如 表中的字段 id 我要讓他查詢顯示為 學號,這樣就叫別名查詢,別名查詢需要用到關(guān)鍵字as(也可以省略)
語法格式 :select id as '起的別名' from exam_result;
select id as '學號',name '姓名',chinese '語文',math '數(shù)學',english '英語' from exam_result;
1.5 去重查詢
對math這列進行去重處理
對某列數(shù)據(jù)去重
語法格式:select distinct 列名 from 表名;
select distinct math from exam_result;
1.6 order by 排序
asc(升序排序)從小到大
desc (降序排序) 從大到小
語法格式:select 列名 from 表名 order by 列名 desc;
單列排序 對數(shù)學成績降序排序
select name,math from exam_result order by math desc;
對別名排序 對總分升序排序
select name,chinese + math + english total from exam_result order total asc;
多個字段排序 對數(shù)學成績,以及id排序
select id,name,math from exam_result order by id desc,math desc;
2.條件查詢(where)
如果查詢語句中沒有設(shè)置條件,就會查詢所有的行信息,在實際應用中,一定要指定查詢條件,對記錄進行過濾
語法格式:select 列名 from 表名 where 條件表達式;
#先取出表中的每條數(shù)據(jù),滿足條件的數(shù)據(jù)就返回,不滿足的就過濾掉
比較運算符:
| 運算符 | 名稱 | 說明 | 示例 |
|---|---|---|---|
| > | 大于 | 比較左側(cè)值是否大于右側(cè)值 | salary > 5000 |
| >= | 大于等于 | 比較左側(cè)值是否大于或等于右側(cè)值 | age >= 18 |
| < | 小于 | 比較左側(cè)值是否小于右側(cè)值 | score < 60 |
| <= | 小于等于 | 比較左側(cè)值是否小于或等于右側(cè)值 | quantity <= 100 |
| = | 等于(NULL 不安全) | 比較兩側(cè)值是否相等NULL = NULL 返回 NULL | status = 'active'NULL = NULL → NULL |
| <=> | 等于(NULL 安全) | 比較兩側(cè)值是否相等(包括 NULL)NULL <=> NULL 返回 TRUE(1) | phone <=> NULLNULL <=> NULL → 1 |
| != 或 <> | 不等于 | 比較兩側(cè)值是否不相等 | department != 'HR'status <> 'expired' |
| BETWEEN a0 AND a1 | 范圍匹配 | 檢查值是否在 [a0, a1] 閉區(qū)間內(nèi)a0 <= value <= a1 返回 TRUE(1) | age BETWEEN 18 AND 30date BETWEEN '2023-01-01' AND '2023-12-31' |
| IN (option, ...) | 集合匹配 | 檢查值是否在指定集合中 | id IN (101, 205, 307)status IN ('active', 'pending') |
| IS NULL | 是否為 NULL | 檢查值是否為 NULL | email IS NULL |
| IS NOT NULL | 是否不為 NULL | 檢查值是否不為 NULL | phone IS NOT NULL |
| LIKE | 模糊匹配 | 使用通配符匹配文本:% - 任意多個字符(含0個)_ - 單個任意字符 |
邏輯運算符:
| 運算符 | 說明 |
| AND | 多個條件必須都為 TRUE(1),結(jié)果才是 TRUE(1) |
| or | 任意一個條件為 TRUE(1), 結(jié)果為 TRUE(1) |
| not | 條件為 TRUE(1),結(jié)果為 FALSE(0) |
注意:
1. WHERE條件可以使用表達式,但不能使用別名。
2. AND的優(yōu)先級高于OR,在同時使用時,需要使用小括號()包裹優(yōu)先執(zhí)行的部分
2.1 基本查詢
查詢英語不及格的同學及英語成績 ( < 60 )
select name,english from exam_result where english < 60;
查詢語文成績好于英語成績的同學
select name ,chinese,english from exam_result where chinese > english;
查詢總分在 200 分以下的同學
select name,chinese + english + math 總分 from exam_result where chinese + english + math <200;
2.2 and與or
查詢語文成績大于80分,且英語成績大于80分的同學
select name,chinese,english from exam_result where chinese > 80 and english > 80;
查詢語文成績大于80分,或英語成績大于80分的同學
select name,chinese,english from exam_result where chinese > 80 or english > 80;
觀察AND 和 OR 的優(yōu)先級
select name,chinese,english from exam_result where chinese > 80 or english > 80 and english > 70; select name,chinese,english from exam_result where (chinese > 80 or english) > 80 and english > 70;
2.3 范圍查詢 in,between...and...
查詢語文成績在 [80, 90] 分的同學及語文成績
select name,chinese from exam_result where chinese between 80 and 90;
使用 AND 也可以實現(xiàn)
select name,chinese from exam_result where chinese >= 80 and chinese <= 90;
2.4 模糊查詢 Like
通過模糊查詢姓孫的同學信息
select name from exam_result where name like '%孫%';
通過模糊查詢孫姓為兩字的名字
select name from exam_result where name like '孫_';
2.5 NULL查詢 is null(is not null)
查詢 有英語成績的同學姓名
select name from exam_result where english is not null;
查詢 沒有英語成績的同學姓名
select name from exam_rseult where exam_result is null;
3.分頁查詢(limit)
limit 關(guān)鍵字的作用
1.limit是限制的意思,用于 限制返回的查詢結(jié)果的行數(shù) (可以通過limit指定查詢多少行數(shù)據(jù))
2.limit 語法是 MySql的方言,用來完成分頁
語法格式:SELECT 字段1,字段2... FROM 表名 LIMIT offset , length;
limit offset , length; 關(guān)鍵字可以接受一個 或者兩個 為0 或者正整數(shù)的參數(shù)
offset 起始行數(shù), 從0開始記數(shù), 如果省略 則默認為 0.
length 返回的行數(shù)
查詢成績表中的前5條數(shù)據(jù)
select * from exam_result limit 5; select * from exam_result limit 0,5;
查詢成績表中從第四條開始,查詢2條
select * from exam_result limit 3,2;
分頁操作,每頁顯示2條數(shù)據(jù)
select * from exam_result 0,2; select * from exam_result 2,2; select * from exam_result 4,2;
本內(nèi)容有錯誤的地方請大家在評論區(qū)指出!我們一起學習進步 ?。?!下一個內(nèi)容我們將學習多表查詢以及事務(wù)管理.....?。?!
到此這篇關(guān)于MySQL表的增刪改查及查詢表中數(shù)據(jù)的三種方式的文章就介紹到這了,更多相關(guān)mysql增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL安裝提示配置信息已損壞請聯(lián)系技術(shù)人員
為了重新安裝MySql,看別人的博客說在注冊表中搜索mysql,全部刪除。再安裝時提示配置信息已損壞,遇到這個問題怎么處理呢,下面小編給大家?guī)砹嗽敿毥鉀Q方法,感興趣的朋友一起看看吧2023-01-01
Prometheus 插件mysql_exporter安裝過程
mysql_exporter是用來收集MysQL或者Mariadb數(shù)據(jù)庫相關(guān)指標的,mysql_exporter需要連接到數(shù)據(jù)庫并有相關(guān)權(quán)限,這篇文章主要介紹了Prometheus插件安裝(mysql_exporter),需要的朋友可以參考下2023-06-06
MySQL實現(xiàn)Upsert(Update or Insert)功能
在數(shù)據(jù)庫操作中,經(jīng)常會遇到這樣的需求,當某條記錄不存在時,需要插入一條新的記錄,如果該記錄已經(jīng)存在,則需要更新這條記錄的某些字段,即Upsert,下面我們就來看看如何在MySQL中實現(xiàn)這一功能2025-07-07
mysql如何實現(xiàn)多行查詢結(jié)果合并成一行
利用函數(shù):group_concat(),實現(xiàn)一個ID對應多個名稱時,原本為多行數(shù)據(jù),把名稱合并成一行2013-12-12
MySQL報錯1067 :Invalid default value for&n
在使用MySQL5.7時,還原數(shù)據(jù)庫的時候報錯,下面就來介紹一下MySQL報錯1067 :Invalid default value for ‘字段名’,具有一定的參考價值,感興趣的可以了解一下2024-05-05
MySQL8.0設(shè)置連接超時及登錄失敗鎖定問題及解決
本文介紹了MySQL中設(shè)置連接超時時間和登錄失敗鎖定的方法,首先通過SQL命令查看并修改連接超時時間,然后檢查并啟用相關(guān)插件,最后配置登錄失敗鎖定2026-05-05

