最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL中CRUD操作及常用查詢語法舉例詳解

 更新時間:2025年10月11日 10:14:49   作者:心酸伴隨人生  
SQL是一種標(biāo)準(zhǔn)化的語言,它允許你在數(shù)據(jù)庫上執(zhí)行操作,如創(chuàng)建項目,查詢內(nèi)容,更新內(nèi)容,并刪除條目等操作,這篇文章主要介紹了MySQL中CRUD操作及常用查詢語法的相關(guān)資料,需要的朋友可以參考下

Mysql-CURD

CRUD : Create(創(chuàng)建), Retrieve(讀取),Update(更新),Delete(刪除)。

1. Create

語法:

INSERT [INTO] table_name [(column [, column] ...)] VALUES (value_list) , (value_list) , ...;

案例:

CREATE TABLE students (
    id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    sn INT NOT NULL UNIQUE COMMENT '學(xué)號',
    name VARCHAR(20) NOT NULL,
    qq VARCHAR(20)
);

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

inser into students values (1, 20250123, 'Jack' , NULL);

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

inser into (sn , name , qq) students values (20250124, 'ali' , "123456789") , (20250125 , "alger" , "23456781");

1.3 插入否則更新

1.3.1 on duplicate key

  • 主鍵 或者 唯一鍵 沒有沖突,則直接插入。

  • 主鍵 或者 唯一鍵 如果沖突,則刪除后再插入。

語法:

INSERT ... ON DUPLICATE KEY UPDATE column = value [, column = value] ...

案例:

insert into students values (1 , 20250126 , "Jack" , "345678912") on duplicate key update sn = 20250126;
  • 0 row affected:表中有沖突數(shù)據(jù),但沖突數(shù)據(jù)的值和 update 的值相等。

  • 1 row affected:表中沒有沖突數(shù)據(jù),數(shù)據(jù)被插入。

  • 2 row affected:表中有沖突數(shù)據(jù),并且數(shù)據(jù)已經(jīng)被更新。

1.3.2 replace

語法:

REPLACE ... ON DUPLICATE KEY UPDATE column = value [, column = value] ...

2. Retrieve

語法:

SELECT
    [DISTINCT] {* | {column [, column] ...}
    [FROM table_name]
    [WHERE ...]
    [ORDER BY column [ASC | DESC], ...]
    LIMIT ...

2.0 select 順序

SQL SELECT 語句的典型執(zhí)行順序為:

  1. FROM(指定數(shù)據(jù)來源表,是查詢的基礎(chǔ))。

  2. WHERE(篩選行,對 FROM 后的結(jié)果進行條件過濾)。

  3. SELECT(指定要查詢的列或表達式)。

  4. ORDER BY(對結(jié)果集排序)。

  5. LIMIT(限制結(jié)果集的行數(shù),多用于分頁等場景)。

案例:

# 案例
CREATE TABLE exam_result (
    id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(20) NOT NULL COMMENT '同學(xué)姓名',
    chinese float DEFAULT 0.0 COMMENT '語文成績',
    math float DEFAULT 0.0 COMMENT '數(shù)學(xué)成績',
    english float DEFAULT 0.0 COMMENT '英語成績'
);
# 插入數(shù)據(jù)
INSERT INTO exam_result (name, chinese, math, english) VALUES
    ('唐三藏', 67, 98, 56),
    ('孫悟空', 87, 78, 77),
    ('豬悟能', 88, 98, 90),
    ('曹孟德', 82, 84, 67),
    ('劉玄德', 55, 85, 45),
    ('孫權(quán)', 70, 73, 78),
    ('宋公明', 75, 65, 30);

2.1 全列查詢

select * from exam_result;

2.2 指定列查詢

select id , name , english from exam_result;

2.3 查詢并計算臨時表達式

select id , name , english + chinese + math from exam_result;

2.4 為2.3起別名

select id , name , english + chinese + math as total from exam_result;
select id , name , english + chinese + math total from exam_result;

2.5 查詢結(jié)果去重

select distinct math from exam_result;

2.6 where 條件

2.6.1 運算符

比較運算符:

運算符說明
>, >=, <, <=大于,大于等于,小于,小于等于
=等于,NULL 不安全,例如 NULL = NULL 的結(jié)果是 NULL
<=>等于,NULL 安全,例如 NULL <=> NULL 的結(jié)果是 TRUE(1)
!=, <>不等于,區(qū)分 NULL 安全與 NULL 不安全
BETWEEN a0 AND a1范圍匹配,[a0, a1],返回 TRUE(1)
IN (option, …)如果是 option 中的任意一個,返回 TRUE(1)
IS NULL是 NULL
IS NOT NULL不是 NULL
LIKE模糊匹配。% 表示任意多個(包括 0 個)任意字符;_ 表示任意一個字符

邏輯運算符:

運算符說明
AND多個條件必須都為 TRUE(1),結(jié)果才是 TRUE(1)
OR任意一個條件為 TRUE(1), 結(jié)果為 TRUE(1)
NOT條件為 TRUE(1),結(jié)果為 FALSE(0)

2.6.2 英語不及格的同學(xué)及英語成績

select name , english from exam_result where english < 60;

2.6.3 語文成績在 [80, 90] 分的同學(xué)及語文成績

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

2.6.4 數(shù)學(xué)成績是 58 或者 59 分的同學(xué)及數(shù)學(xué)成績

select name , math from exam_result where math = 58 or math = 59;
select name , math from exam_result where math in (58 , 59);

2.6.5 姓孫的同學(xué)及孫某同學(xué)

select name from exam_result where name like "孫%"; # 孫悟空、孫權(quán)
select name from exam_result where name like "孫_"; # 孫權(quán)

2.6.5 語文成績好于英語成績的同學(xué)

select name , chinese , english from exam_result where chinese > english;

2.6.6 總分在200分以下的同學(xué)

select name , chinese + math + english total from exam_result where chinese + math + english < 200;

注意:total 不可以在 where 子句中使用。

2.6.7 不是孫某同學(xué)

select name from exam_result where name not like "孫_";

2.6.8 name不是NULL的

select name from exam_result where name is not null;

2.7 order by

  • asc 升序(默認(rèn))。

  • desc 降序。

語法:

SELECT ... FROM table_name [WHERE ...] ORDER BY column [ASC|DESC], [...];

2.7.1 同學(xué)及數(shù)學(xué)成績,按數(shù)學(xué)成績升序顯示

select name , math from exam_result order by math;

2.7.2 查詢同學(xué)各門成績,依次按 數(shù)學(xué)降序,英語升序,語文升序的方式顯示

select name , math , english , chinese from exam_result order by math desc , english asc , chinese asc;

2.7.3 查詢姓孫的同學(xué)或者姓曹的同學(xué)數(shù)學(xué)成績,結(jié)果按數(shù)學(xué)成績由高到低顯示

select name , math from exam_result where name like "孫%" or name like "曹%" order by math desc;

2.8 limit

語法:

# 起始下標(biāo)為 0
# 從 0 開始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
# 從 s 開始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
# 從 s 開始,篩選 n 條結(jié)果,比第二種用法更明確,建議使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;

2.8.1 按 id 進行分頁,每頁 3 條記錄,分別顯示 第 1、2、3 頁。

select id , name , math , chinese , english from exam_result order by id asc limit 0,3;
select id , name , math , chinese , english from exam_result order by id asc limit 3,3;
select id , name , math , chinese , english from exam_result order by id asc limit 6,3;

3. Update

語法:

UPDATE table_name SET column = expr [, column = expr ...]
    [WHERE ...] [ORDER BY ...] [LIMIT ...]

對查詢到的結(jié)果進行列值更新。

3.1 將孫悟空同學(xué)的數(shù)學(xué)成績變更為 80 分

update exam_result set math = 80 where name = "孫悟空";

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

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

3.3 將所有人的數(shù)學(xué)成績更新為原來的2倍

update exam_result set math = math * 2;

注意:update通常需要判斷條件,更新全表的語句慎用!

4. Delete

語法:

DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]

4.1 刪除孫悟空

delete from exam_result where name = "孫悟空";

4.2 刪除整表內(nèi)容

delete from exam_result;

慎用!

5. 插入查詢結(jié)果

語法:

INSERT INTO table_name [(column [, column ...])] SELECT ...

1?? 創(chuàng)建一個表,結(jié)構(gòu)復(fù)制 exam_result 表。

create table no_duplicate_table like exam_result;

2?? 將 exam_result 查詢的結(jié)果插入到 no_duplicate_table 表。

insert into no_duplicate_table select id , name , chinese , math , english from exam_result; 

6. 聚合函數(shù)

函數(shù)說明
COUNT([DISTINCT] expr)返回查詢到的數(shù)據(jù)的 數(shù)量
SUM([DISTINCT] expr)返回查詢到的數(shù)據(jù)的 總和,不是數(shù)字沒有意義
AVG([DISTINCT] expr)返回查詢到的數(shù)據(jù)的 平均值,不是數(shù)字沒有意義
MAX([DISTINCT] expr)返回查詢到的數(shù)據(jù)的 最大值,不是數(shù)字沒有意義
MIN([DISTINCT] expr)返回查詢到的數(shù)據(jù)的 最小值,不是數(shù)字沒有意義

6.1 統(tǒng)計表中有多少行

select count(*) from exam_result;
  • NULL不計入結(jié)果。

  • select count(distinct col) from table-name 統(tǒng)計去重的結(jié)果。

6.2 統(tǒng)計數(shù)學(xué)總成績

select sum(math) from exam_result;

6.3 統(tǒng)計不及格同學(xué)的數(shù)學(xué)總成績

select sum(math) from exam_result where math < 60;

6.4 統(tǒng)計平均分

select avg(chinese + math + english) from exam_result;

6.5 返回英語最高分

select max(english) from exam_result;

6.6 返回數(shù)學(xué)最低分

select min(math) from exam_result;

注意:select name, min(math) from exam_result;

報錯信息:只有按 name 分組后才可以這樣使用,可通過 where order by limit方式查詢。

7. group by子句

select 中使用 group by子句可以對指定列進行分組查詢,通常與聚合函數(shù)一起使用。

準(zhǔn)備工作:創(chuàng)建一個雇員信息表(Oracle 9i經(jīng)典測試表)

DROP database IF EXISTS `scott`;
CREATE database IF NOT EXISTS `scott` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

USE `scott`;

DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
  `deptno` int(2) unsigned zerofill NOT NULL COMMENT ' 部門編號 ',
  `dname` varchar(14) DEFAULT NULL COMMENT ' 部門名稱 ',
  `loc` varchar(13) DEFAULT NULL COMMENT ' 部門所在地點 ' 
);


DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
  `empno` int(6) unsigned zerofill NOT NULL COMMENT '雇員編號',
  `ename` varchar(10) DEFAULT NULL COMMENT '雇員姓名',
  `job` varchar(9) DEFAULT NULL COMMENT '雇員職位',
  `mgr` int(4) unsigned zerofill DEFAULT NULL COMMENT '雇員領(lǐng)導(dǎo)編號',
  `hiredate` datetime DEFAULT NULL COMMENT '雇傭時間',
  `sal` decimal(7,2) DEFAULT NULL COMMENT '工資月薪',
  `comm` decimal(7,2) DEFAULT NULL COMMENT '獎金',
  `deptno` int(2) unsigned zerofill DEFAULT NULL COMMENT '部門編號'
);


DROP TABLE IF EXISTS `salgrade`;
CREATE TABLE `salgrade` (
  `grade` int(11) DEFAULT NULL COMMENT '等級',
  `losal` int(11) DEFAULT NULL COMMENT '此等級最低工資',
  `hisal` int(11) DEFAULT NULL COMMENT '此等級最高工資'
);


insert into dept (deptno, dname, loc)
values (10, 'ACCOUNTING', 'NEW YORK');
insert into dept (deptno, dname, loc)
values (20, 'RESEARCH', 'DALLAS');
insert into dept (deptno, dname, loc)
values (30, 'SALES', 'CHICAGO');
insert into dept (deptno, dname, loc)
values (40, 'OPERATIONS', 'BOSTON');

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, null, 20);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7844, 'TURNER', 'SALESMAN', 7698,'1981-09-08', 1500, 0, 30);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100, null, 20);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);

insert into salgrade (grade, losal, hisal) values (1, 700, 1200);
insert into salgrade (grade, losal, hisal) values (2, 1201, 1400);
insert into salgrade (grade, losal, hisal) values (3, 1401, 2000);
insert into salgrade (grade, losal, hisal) values (4, 2001, 3000);
insert into salgrade (grade, losal, hisal) values (5, 3001, 9999);

在 Oracle 9i 的經(jīng)典測試表(SCOTT 用戶下的 EMPDEPT 表)中,deptno 字段的外鍵關(guān)系是邏輯上的、約定俗成的,而不是通過數(shù)據(jù)庫物理外鍵約束(Foreign Key Constraint)強制實現(xiàn)的。

7.1 顯示每個部門的平均工資和最高工資

select deptno , avg(sal) , max(sal) from emp group by deptno;
  • SELECT 子句中的字段,要么必須包含在 GROUP BY 子句中,要么必須被包含在聚合函數(shù)(如 AVG, MAX, SUM, COUNT 等)中。

  • group by理解為分組,也可以理解為分表。

7.2 顯示每個部門不同崗位的平均工資和最低工資

select deptno , job, avg(sal) , min(sal) from emp group by deptno , job;

7.3 having

having 和 group by 配合使用,對 group by 結(jié)果進行過濾。

7.3.1 查詢每個部門的平均工資,并只顯示那些平均工資低于 2000 的部門

select avg(sal) from emp group by deptno having avg(sal) < 2000;

7.3 having 和 where 的區(qū)別

7.3.1 查詢每個部門的平均工資(但不包括員工名 SMITH 的員工數(shù)據(jù))。

select deptno , avg(sal) from emp where ename != "SMITH" group by deptno; 

簡單的比喻:

  • WHERE原材料質(zhì)檢員,在加工(分組聚合)前就把爛蘋果(不合格的行)扔掉。

  • HAVING成品質(zhì)檢員,在加工(分組聚合)完成后,檢查做好的蘋果罐頭(分組結(jié)果),把不合格的整批罐頭扔掉。

where 之后,也是一個表。where 本質(zhì)是先過濾出你想要分組的表,之后再通過 group by 進行分組。

8. SQL查詢中各個關(guān)鍵字的執(zhí)行順序

from > on > join > where > group by > with > having > select > distinct > order by > limit

總結(jié) 

到此這篇關(guān)于MySQL中CRUD操作及常用查詢語法舉例詳解的文章就介紹到這了,更多相關(guān)MySQL CRUD操作及查詢語法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mysql主從同步的實現(xiàn)原理

    Mysql主從同步的實現(xiàn)原理

    這篇文章主要介紹了Mysql主從同步的實現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • mysql數(shù)據(jù)庫 主從復(fù)制的配置方法

    mysql數(shù)據(jù)庫 主從復(fù)制的配置方法

    本文主要介紹 mysql數(shù)據(jù)庫 主從負(fù)責(zé)的配置方法,在做數(shù)據(jù)庫開發(fā)的時候有時候會遇到,這里做出詳細(xì)流程,大家可以參考下
    2016-07-07
  • MySQL遞歸CTE案例解析

    MySQL遞歸CTE案例解析

    MySQL 8.0 引入的遞歸CTE(公共表表達式),為層級數(shù)據(jù)查詢提供了簡潔高效的解決方案,本文將從基礎(chǔ)概念出發(fā),逐步深入遞歸CTE的語法、實戰(zhàn)場景、常見問題與優(yōu)化技巧,幫助你徹底掌握這一強大工具,感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • MySQLexplain之possible_keys、key及key_len詳解

    MySQLexplain之possible_keys、key及key_len詳解

    這篇文章主要介紹了MySQLexplain之possible_keys、key及key_len的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • SQL查詢語句執(zhí)行的過程

    SQL查詢語句執(zhí)行的過程

    這篇文章主要介紹了SQL查詢語句執(zhí)行的過程,文章圍繞主題展開SQL查詢語句的相關(guān)資料,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05
  • Windows下mysql修改root密碼的4種方法

    Windows下mysql修改root密碼的4種方法

    這篇文章主要為大家詳細(xì)介紹了windows下mysql修改root密碼的4種方法,大家可以根據(jù)的自己的實際情況進行選擇,感興趣的小伙伴們可以參考一下
    2016-05-05
  • MySQL設(shè)置密碼復(fù)雜度策略的完整步驟(附代碼示例)

    MySQL設(shè)置密碼復(fù)雜度策略的完整步驟(附代碼示例)

    MySQL密碼策略還可能包括密碼復(fù)雜度的檢查,如是否要求密碼包含大寫字母、小寫字母、數(shù)字和特殊字符等,這篇文章主要介紹了MySQL設(shè)置密碼復(fù)雜度策略的完整步驟,需要的朋友可以參考下
    2025-08-08
  • Windows下實現(xiàn)MySQL自動備份的批處理(復(fù)制目錄或mysqldump備份)

    Windows下實現(xiàn)MySQL自動備份的批處理(復(fù)制目錄或mysqldump備份)

    Windows下實現(xiàn)MySQL自動備份的批處理,新建目錄并復(fù)制壓縮,結(jié)合windows計劃任務(wù)方便實現(xiàn)每天的自動備份
    2012-05-05
  • mysql 服務(wù)完全卸載技巧

    mysql 服務(wù)完全卸載技巧

    完整的把MYSQL服務(wù)刪除的步驟。
    2009-06-06
  • mysql正確刪除數(shù)據(jù)的方法(drop,delete,truncate)

    mysql正確刪除數(shù)據(jù)的方法(drop,delete,truncate)

    這篇文章主要給大家介紹了關(guān)于mysql正確刪除數(shù)據(jù)的相關(guān)資料,DELETE語句是MySQL中最常用的刪除數(shù)據(jù)的方式之一,但也有幾種其他方法來實現(xiàn),需要的朋友可以參考下
    2023-10-10

最新評論

郧西县| 金阳县| 广汉市| 积石山| 平谷区| 六枝特区| 武安市| 五峰| 平度市| 宜宾县| 辽源市| 前郭尔| 广德县| 玛纳斯县| 麻城市| 凤庆县| 永康市| 钟山县| 奉化市| 来宾市| 荔波县| 城口县| 特克斯县| 洪洞县| 巨野县| 二手房| 望奎县| 长葛市| 石屏县| 南召县| 临夏市| 田阳县| 扶沟县| 樟树市| 昭平县| 明光市| 杂多县| 邛崃市| 南康市| 财经| 苍梧县|