網(wǎng)站建設找美橙互聯(lián)百度關鍵詞怎么刷上去
1. MySQL概述
首先來了解一下什么是數(shù)據(jù)庫。
-
數(shù)據(jù)庫:英文為 DataBase,簡稱DB,它是存儲和管理數(shù)據(jù)的倉庫。
像我們?nèi)粘TL問的電商網(wǎng)站京東,企業(yè)內(nèi)部的管理系統(tǒng)OA、ERP、CRM這類的系統(tǒng),以及大家每天都會刷的頭條、抖音類的app,那這些大家所看到的數(shù)據(jù),其實都是存儲在數(shù)據(jù)庫中的。最終這些數(shù)據(jù),只是在瀏覽器或app中展示出來而已,最終數(shù)據(jù)的存儲和管理都是數(shù)據(jù)庫負責的。
數(shù)據(jù)是存儲在數(shù)據(jù)庫中的,那我們要如何來操作數(shù)據(jù)庫以及數(shù)據(jù)庫中所存放的數(shù)據(jù)呢?
那這里呢,會涉及到一個軟件,那就是數(shù)據(jù)庫管理系統(tǒng)。
數(shù)據(jù)庫管理系統(tǒng)(DataBase Management System,簡稱DBMS),是操作和管理數(shù)據(jù)庫的大型軟件
將來我們只需要操作這個軟件,就可以通過這個軟件來操縱和管理數(shù)據(jù)庫了。
此時又出現(xiàn)一個問題:DBMS這個軟件怎么知道要操作的是哪個數(shù)據(jù)庫、哪個數(shù)據(jù)呢?是對數(shù)據(jù)做修改還是查詢呢?
需要給DBMS軟件發(fā)送一條指令,告訴這個軟件我們要執(zhí)行的是什么樣的操作,要對哪個數(shù)據(jù)進行操作。而這個指令就是SQL語句。
-
SQL(Structured Query Language,簡稱SQL):結構化查詢語言,它是操作關系型數(shù)據(jù)庫的編程語言,定義了一套操作關系型數(shù)據(jù)庫的統(tǒng)一標準。
我們學習數(shù)據(jù)庫開發(fā),最為重要的就是學習SQL語句 。
關系型數(shù)據(jù)庫:我們后面會詳細講解,現(xiàn)在大家只需要知道我們學習的數(shù)據(jù)庫屬于關系型數(shù)據(jù)庫即可。
結論:程序員給數(shù)據(jù)庫管理系統(tǒng)(DBMS)發(fā)送SQL語句,再由數(shù)據(jù)庫管理系統(tǒng)操作數(shù)據(jù)庫當中的數(shù)據(jù)。
2. SQL語句
2.1 DDL語句
2.1.1 數(shù)據(jù)庫操作
我們在進行數(shù)據(jù)庫設計,需要使用到剛才所介紹SQL分類中的DDL語句。
DDL英文全稱是Data Definition Language(數(shù)據(jù)定義語言),用來定義數(shù)據(jù)庫對象(數(shù)據(jù)庫、表)。
DDL中數(shù)據(jù)庫的常見操作:查詢、創(chuàng)建、使用、刪除。
2.1.1.1 查詢數(shù)據(jù)庫
-
查詢所有數(shù)據(jù)庫
show databases;
命令行中執(zhí)行效果如下:
-
查詢當前數(shù)據(jù)庫
select database();
命令行中執(zhí)行效果如圖:
我們要操作某一個數(shù)據(jù)庫,必須要切換到對應的數(shù)據(jù)庫中。
通過指令:select database() ,就可以查詢到當前所處的數(shù)據(jù)庫
2.1.1.2 創(chuàng)建數(shù)據(jù)庫
-
語法:
create database [ if not exists ] 數(shù)據(jù)庫名 [default charset utf8mb4];
創(chuàng)建數(shù)據(jù)庫時,可以不指定字符集。 因為在MySQL8版本之后,默認的字符集就是 utf8mb4。
-
案例: 創(chuàng)建一個itcast數(shù)據(jù)庫。
create database itcast;
2.1.1.3 使用數(shù)據(jù)庫
-
語法:
use 數(shù)據(jù)庫名 ;
我們要操作某一個數(shù)據(jù)庫下的表時,就需要通過該指令,切換到對應的數(shù)據(jù)庫下,否則不能操作。
-
案例:切換到itcast數(shù)據(jù)庫
use itcast;
2.1.1.4 刪除數(shù)據(jù)庫
-
語法:
drop database [ if exists ] 數(shù)據(jù)庫名 ;
-
如果刪除一個不存在的數(shù)據(jù)庫,將會報錯。
-
可以加上參數(shù) if exists ,如果數(shù)據(jù)庫存在,再執(zhí)行刪除,否則不執(zhí)行刪除
-
案例:刪除itcast數(shù)據(jù)庫
drop database if exists itcast; -- itcast數(shù)據(jù)庫存在時刪除
說明:上述語法中的database,也可以替換成 schema
如:create schema db01;
如:show schemas;
2.1.2 表操作
學習完了DDL語句當中關于數(shù)據(jù)庫的操作之后,接下來我們繼續(xù)學習DDL語句當中關于表結構的操作。
關于表結構的操作也是包含四個部分:創(chuàng)建表、查詢表、修改表、刪除表。
2.1.2.1?創(chuàng)建
-
語法:
create table 表名(字段1 字段1類型 [約束] [comment 字段1注釋 ],字段2 字段2類型 [約束] [comment 字段2注釋 ],......字段n 字段n類型 [約束] [comment 字段n注釋 ] ) [ comment 表注釋 ] ;
-
注意: [ ] 中的內(nèi)容為可選參數(shù); 最后一個字段后面沒有逗號
-
案例:創(chuàng)建tb_user表
-
對應的結構如下:
-
-
建表語句:
create table tb_user (id int comment 'ID,唯一標識', # id是一行數(shù)據(jù)的唯一標識(不能重復)username varchar(20) comment '用戶名',name varchar(10) comment '姓名',age int comment '年齡',gender char(1) comment '性別'
) comment '用戶表';
-
數(shù)據(jù)表創(chuàng)建完成,接下來我們還需要測試一下是否可以往這張表結構當中來存儲數(shù)據(jù)。
??雙擊打開tb_user表結構,大家會發(fā)現(xiàn)里面沒有數(shù)據(jù):
添加數(shù)據(jù):
我們之前提到過:id字段是一行數(shù)據(jù)的唯一標識,不能有重復值。但是現(xiàn)在數(shù)據(jù)表中有兩個相同的id值,這是為什么呢?
-
其實我們現(xiàn)在創(chuàng)建表結構的時候, id這個字段我們只加了一個備注信息說明它是一個唯一標識,但是在數(shù)據(jù)庫層面呢,并沒有去限制字段存儲的數(shù)據(jù)。所以id這個字段沒有起到唯一標識的作用。
想要限制字段所存儲的數(shù)據(jù),就需要用到數(shù)據(jù)庫中的約束。
2.1.2.2 約束
-
概念:所謂約束就是作用在表中字段上的規(guī)則,用于限制存儲在表中的數(shù)據(jù)。
-
作用:就是來保證數(shù)據(jù)庫當中數(shù)據(jù)的正確性、有效性和完整性。(后面的學習會驗證這些)
-
在MySQL數(shù)據(jù)庫當中,提供了以下5種約束:
注意:約束是作用于表中字段上的,可以在創(chuàng)建表/修改表的時候添加約束。
-
案例:創(chuàng)建tb_user表,對應的結構如下:
在上述的表結構中:
-
id 是一行數(shù)據(jù)的 唯一標識
-
username 用戶名字段是非空且唯一的
-
name 姓名字段是不允許存儲空值的
-
gender 性別字段是有默認值,默認為男
建表語句:
create table tb_user (id int primary key comment 'ID,唯一標識', username varchar(20) not null unique comment '用戶名',name varchar(10) not null comment '姓名',age int comment '年齡',gender char(1) default '男' comment '性別'
) comment '用戶表';
數(shù)據(jù)表創(chuàng)建完成,接下來測試一下表中字段上的約束是否生效
大家有沒有發(fā)現(xiàn)一個問題:id字段下存儲的值,如果由我們自己來維護會比較麻煩(必須保證值的唯一性)。MySQL數(shù)據(jù)庫為了解決這個問題,給我們提供了一個關鍵字:auto_increment(自動增長)
主鍵自增:auto_increment
每次插入新的行記錄時,數(shù)據(jù)庫自動生成id字段(主鍵)下的值
具有auto_increment的數(shù)據(jù)列是一個正數(shù)序列開始增長(從1開始自增)
create table tb_user (id int primary key auto_increment comment 'ID,唯一標識', #主鍵自動增長username varchar(20) not null unique comment '用戶名',name varchar(10) not null comment '姓名',age int comment '年齡',gender char(1) default '男' comment '性別'
) comment '用戶表';
2.1.2.3 數(shù)據(jù)類型
在上面建表語句中,我們在指定字段的數(shù)據(jù)類型時,用到了int 、varchar、char,那么在MySQL中除了以上的數(shù)據(jù)類型,還有哪些常見的數(shù)據(jù)類型呢? 接下來,我們就來詳細介紹一下MySQL的數(shù)據(jù)類型。
MySQL中的數(shù)據(jù)類型有很多,主要分為三類:數(shù)值類型、字符串類型、日期時間類型。
1). 數(shù)值類型
-
示例:
-- 年齡字段 ---不會出現(xiàn)負數(shù), 而且人的年齡不會太大age tinyint unsigned-- 分數(shù) ---總分100分, 最多出現(xiàn)一位小數(shù)score double(4,1)
2). 字符串類型
char 與 varchar 都可以描述字符串,char是定長字符串,指定長度多長,就占用多少個字符,和字段值的長度無關 。而varchar是變長字符串,指定的長度為最大占用長度 。相對來說,char的性能會更高些。
示例: 用戶名 username ---長度不定, 最長不會超過50username varchar(50)手機號 phone ---固定長度為11phone char(11)
3). 日期時間類型
示例: 生日字段 birthday ---生日只需要年月日 birthday date創(chuàng)建時間 createtime --- 需要精確到時分秒createtime datetime
2.1.2.4 表結構設計-案例
需求:根據(jù)產(chǎn)品原型/需求創(chuàng)建表((設計合理的數(shù)據(jù)類型、長度、約束)
產(chǎn)品原型及需求如下:
1). 列表展示
2). 新增員工
3). 需求說明及字段限制
步驟:
-
閱讀產(chǎn)品原型及需求文檔,看看里面涉及到哪些字段。
-
查看需求文檔說明,確認各個字段的類型以及字段存儲數(shù)據(jù)的長度限制。
-
在頁面原型中描述的基礎字段的基礎上,再增加額外的基礎字段。
使用SQL創(chuàng)建表:
create table emp(id int unsigned primary key auto_increment comment 'ID,主鍵',username varchar(20) not null unique comment '用戶名',password varchar(32) not null comment '密碼',name varchar(10) not null comment '姓名',gender tinyint unsigned not null comment '性別, 1:男, 2:女',phone char(11) not null unique comment '手機號',job tinyint unsigned comment '職位, 1:班主任,2:講師,3:學工主管,4:教研主管,5:咨詢師',salary int unsigned comment '薪資',image varchar(255) comment '頭像',entry_date date comment '入職日期',create_time datetime comment '創(chuàng)建時間',update_time datetime comment '修改時間'
) comment '員工表';
除了使用SQL語句創(chuàng)建表外,我們還可以借助于圖形化界面來創(chuàng)建表結構,這種創(chuàng)建方式會更加直觀、更加方便。
閱讀頁面原型及需求文檔
基于頁面原則和需求文檔,確定原型字段(類型、長度限制、約束)
再增加表設計所需要的業(yè)務基礎字段(id、create_time、update_time)
create_time:記錄的是當前這條數(shù)據(jù)插入的時間。
update_time:記錄當前這條數(shù)據(jù)最后更新的時間。
2.1.2.5 表操作-其他操作
上面講解了表結構的創(chuàng)建、數(shù)據(jù)類型、設計表的流程,接下來,再來講解表結構的查詢、修改、刪除操作 。
-
查詢數(shù)據(jù)庫表的具體的語法:
-- 查詢當前數(shù)據(jù)庫的所有表
show tables;-- 查看指定的表結構
desc 表名 ; -- 可以查看指定表的字段、字段的類型、是否可以為NULL、是否存在默認值等信息-- 查詢指定表的建表語句
show create table 表名 ;
-
修改數(shù)據(jù)庫表結構的具體語法:
添加字段
-- 添加字段
alter table 表名 add 字段名 類型(長度) [comment 注釋] [約束];-- 比如: 為tb_emp表添加字段qq,字段類型為 varchar(11)
alter table tb_emp add qq varchar(11) comment 'QQ號碼';
修改字段
-- 修改字段類型
alter table 表名 modify 字段名 新數(shù)據(jù)類型(長度);-- 比如: 修改qq字段的字段類型,將其長度由11修改為13
alter table tb_emp modify qq varchar(13) comment 'QQ號碼';
-- 修改字段名,字段類型
alter table 表名 change 舊字段名 新字段名 類型(長度) [comment 注釋] [約束];-- 比如: 修改qq字段名為 qq_num,字段類型varchar(13)
alter table tb_emp change qq qq_num varchar(13) comment 'QQ號碼';
刪除字段
-- 刪除字段
alter table 表名 drop 字段名;-- 比如: 刪除tb_emp表中的qq_num字段
alter table tb_emp drop qq_num;
修改表名
-- 修改表名
rename table 表名 to 新表名;-- 比如: 將當前的emp表的表名修改為tb_emp
rename table emp to tb_emp;
刪除表結構
-- 刪除表
drop table [ if exists ] 表名;-- 比如:如果tb_emp表存在,則刪除tb_emp表
drop table if exists tb_emp; -- 在刪除表時,表中的全部數(shù)據(jù)也會被刪除。
關于表結構的查看、修改、刪除操作,工作中一般都是直接基于圖形化界面操作。
2.2 DML語句
DML英文全稱是Data Manipulation Language(數(shù)據(jù)操作語言),用來對數(shù)據(jù)庫中表的數(shù)據(jù)記錄進行增、刪、改操作。
-
添加數(shù)據(jù)(INSERT)
-
修改數(shù)據(jù)(UPDATE)
-
刪除數(shù)據(jù)(DELETE)
2.2.1 增加(insert)
2.2.1.1 語法
-
向指定字段添加數(shù)據(jù)
insert into 表名 (字段名1, 字段名2) values (值1, 值2);
-
全部字段添加數(shù)據(jù)
insert into 表名 values (值1, 值2, ...);
-
批量添加數(shù)據(jù)(指定字段)
insert into 表名 (字段名1, 字段名2) values (值1, 值2), (值1, 值2);
-
批量添加數(shù)據(jù)(全部字段)
insert into 表名 values (值1, 值2, ...), (值1, 值2, ...);
2.2.1.2?案例演示
-
案例1:向emp表的username, name, gender, phone, create_time, update_time字段插入數(shù)據(jù)
-- 因為設計表時create_time, update_time兩個字段不能為NULL,所以也做為要插入的字段
insert into emp(username, name, gender, phone, create_time, update_time)
values ('wuji', '張無忌', 1, '13309091231', now(), now());
-
案例2:向emp表的所有字段插入數(shù)據(jù)
insert into emp2(id, username, password, name, gender, phone, job, salary, image, entry_date, create_time, update_time)values (1,'shinaian','123456','施耐庵',1,'13309090001',4,15000,'1.jpg','2000-01-01',now(),now()),
-
案例3:批量向emp表的username、name、gender字段插入數(shù)據(jù)
insert into emp(username, name, gender, phone, create_time, update_time)
values ('Tom1', '湯姆1', 1, '13309091231', now(), now()),('Tom2', '湯姆2', 1, '13309091232', now(), now());
insert操作的注意事項:
插入數(shù)據(jù)時,指定的字段順序需要與值的順序是一一對應的。
字符串和日期型數(shù)據(jù)應該包含在引號中。
插入的數(shù)據(jù)大小,應該在字段的規(guī)定范圍內(nèi)。
2.2.2 修改(update)
2.2.2.1 語法
update 表名 set 字段名1 = 值1 , 字段名2 = 值2 , .... [where 條件] ;
2.2.2.2 案例演示
-
案例1:將emp表中id為1的員工,姓名name字段更新為'張三'
update emp set name='張三', update_time=now() where id=1;
-
案例2:將emp表的所有員工入職日期更新為'2010-01-01'
update emp set entry_date='2010-01-01', update_time=now();
注意事項:
修改語句的條件可以有,也可以沒有,如果沒有條件,則會修改整張表的所有數(shù)據(jù)。
在修改數(shù)據(jù)時,一般需要同時修改公共字段update_time,將其修改為當前操作時間。
2.2.3 刪除(delete)
2.2.3.1 語法
delete form 表名 [where 條件];
2.2.3.2 案例演示
-
案例1:刪除emp表中id為1的員工
delete from emp where id = 1;
-
案例2:刪除emp表中所有員工
delete from tb_emp;
注意事項:
DELETE 語句的條件可以有,也可以沒有,如果沒有條件,則會刪除整張表的所有數(shù)據(jù)。
DELETE 語句不能刪除某一個字段的值(可以使用UPDATE,將該字段值置為NULL即可)。
當進行刪除全部數(shù)據(jù)操作時,會提示詢問是否確認刪除所有數(shù)據(jù),直接點擊Execute即可。
2.3 DQL語句
2.3.1 介紹
DQL英文全稱是Data Query Language(數(shù)據(jù)查詢語言),用來查詢數(shù)據(jù)庫表中的記錄。
查詢關鍵字:SELECT
查詢操作是所有SQL語句當中最為常見,也是最為重要的操作。在一個正常的業(yè)務系統(tǒng)中,查詢操作的使用頻次是要遠高于增刪改操作的。當我們打開某個網(wǎng)站或APP所看到的展示信息,都是通過從數(shù)據(jù)庫中查詢得到的,而在這個查詢過程中,還會涉及到條件、排序、分頁等操作。
2.3.2 語法
DQL查詢語句,語法結構如下:
SELECT字段列表
FROM表名列表
WHERE條件列表
GROUP BY分組字段列表
HAVING分組后條件列表
ORDER BY排序字段列表
LIMIT分頁參數(shù)
我們今天會將上面的完整語法拆分為以下幾個部分學習:
-
基本查詢(不帶任何條件)
-
條件查詢(where)
-
分組查詢(group by)
-
排序查詢(order by)
-
分頁查詢(limit)
準備一些測試數(shù)據(jù)用于查詢操作:
create table emp(id int unsigned primary key auto_increment comment 'ID,主鍵',username varchar(20) not null unique comment '用戶名',password varchar(32) not null comment '密碼',name varchar(10) not null comment '姓名',gender tinyint unsigned not null comment '性別, 1:男, 2:女',phone char(11) not null unique comment '手機號',job tinyint unsigned comment '職位, 1:班主任,2:講師,3:學工主管,4:教研主管,5:咨詢師',salary int unsigned comment '薪資',image varchar(300) comment '頭像',entry_date date comment '入職日期',create_time datetime comment '創(chuàng)建時間',update_time datetime comment '修改時間'
) comment '員工表';-- 準備測試數(shù)據(jù)
INSERT INTO emp(id, username, password, name, gender, phone, job, salary, image, entry_date, create_time, update_time)
VALUES (1,'shinaian','123456','施耐庵',1,'13309090001',4,15000,'1.jpg','2000-01-01','2024-04-11 16:35:33','2024-04-11 16:35:35'),(2,'songjiang','123456','宋江',1,'13309090002',2,8600,'2.jpg','2015-01-01','2024-04-11 16:35:33','2024-04-11 16:35:37'),(3,'lujunyi','123456','盧俊義',1,'13309090003',2,8900,'3.jpg','2008-05-01','2024-04-11 16:35:33','2024-04-11 16:35:39'),(4,'wuyong','123456','吳用',1,'13309090004',2,9200,'4.jpg','2007-01-01','2024-04-11 16:35:33','2024-04-11 16:35:41'),(5,'gongsunsheng','123456','公孫勝',1,'13309090005',2,9500,'5.jpg','2012-12-05','2024-04-11 16:35:33','2024-04-11 16:35:43'),(6,'huosanniang','123456','扈三娘',2,'13309090006',3,6500,'6.jpg','2013-09-05','2024-04-11 16:35:33','2024-04-11 16:35:45'),(7,'chaijin','123456','柴進',1,'13309090007',1,4700,'7.jpg','2005-08-01','2024-04-11 16:35:33','2024-04-11 16:35:47'),(8,'likui','123456','李逵',1,'13309090008',1,4800,'8.jpg','2014-11-09','2024-04-11 16:35:33','2024-04-11 16:35:49'),(9,'wusong','123456','武松',1,'13309090009',1,4900,'9.jpg','2011-03-11','2024-04-11 16:35:33','2024-04-11 16:35:51'),(10,'lichong','123456','林沖',1,'13309090010',1,5000,'10.jpg','2013-09-05','2024-04-11 16:35:33','2024-04-11 16:35:53'),(11,'huyanzhuo','123456','呼延灼',1,'13309090011',2,9700,'11.jpg','2007-02-01','2024-04-11 16:35:33','2024-04-11 16:35:55'),(12,'xiaoliguang','123456','小李廣',1,'13309090012',2,10000,'12.jpg','2008-08-18','2024-04-11 16:35:33','2024-04-11 16:35:57'),(13,'yangzhi','123456','楊志',1,'13309090013',1,5300,'13.jpg','2012-11-01','2024-04-11 16:35:33','2024-04-11 16:35:59'),(14,'shijin','123456','史進',1,'13309090014',2,10600,'14.jpg','2002-08-01','2024-04-11 16:35:33','2024-04-11 16:36:01'),(15,'sunerniang','123456','孫二娘',2,'13309090015',2,10900,'15.jpg','2011-05-01','2024-04-11 16:35:33','2024-04-11 16:36:03'),(16,'luzhishen','123456','魯智深',1,'13309090016',2,9600,'16.jpg','2010-01-01','2024-04-11 16:35:33','2024-04-11 16:36:05'),(17,'liying','12345678','李應',1,'13309090017',1,5800,'17.jpg','2015-03-21','2024-04-11 16:35:33','2024-04-11 16:36:07'),(18,'shiqian','123456','時遷',1,'13309090018',2,10200,'18.jpg','2015-01-01','2024-04-11 16:35:33','2024-04-11 16:36:09'),(19,'gudasao','123456','顧大嫂',2,'13309090019',2,10500,'19.jpg','2008-01-01','2024-04-11 16:35:33','2024-04-11 16:36:11'),(20,'ruanxiaoer','123456','阮小二',1,'13309090020',2,10800,'20.jpg','2018-01-01','2024-04-11 16:35:33','2024-04-11 16:36:13'),(21,'ruanxiaowu','123456','阮小五',1,'13309090021',5,5200,'21.jpg','2015-01-01','2024-04-11 16:35:33','2024-04-11 16:36:15'),(22,'ruanxiaoqi','123456','阮小七',1,'13309090022',5,5500,'22.jpg','2016-01-01','2024-04-11 16:35:33','2024-04-11 16:36:17'),(23,'ruanji','123456','阮籍',1,'13309090023',5,5800,'23.jpg','2012-01-01','2024-04-11 16:35:33','2024-04-11 16:36:19'),(24,'tongwei','123456','童威',1,'13309090024',5,5000,'24.jpg','2006-01-01','2024-04-11 16:35:33','2024-04-11 16:36:21'),(25,'tongmeng','123456','童猛',1,'13309090025',5,4800,'25.jpg','2002-01-01','2024-04-11 16:35:33','2024-04-11 16:36:23'),(26,'yanshun','123456','燕順',1,'13309090026',5,5400,'26.jpg','2011-01-01','2024-04-11 16:35:33','2024-04-11 16:36:25'),(27,'lijun','123456','李俊',1,'13309090027',5,6600,'27.jpg','2004-01-01','2024-04-11 16:35:33','2024-04-11 16:36:27'),(28,'lizhong','123456','李忠',1,'13309090028',5,5000,'28.jpg','2007-01-01','2024-04-11 16:35:33','2024-04-11 16:36:29'),(29,'songqing','123456','宋清',1,'13309090029',5,5100,'29.jpg','2020-01-01','2024-04-11 16:35:33','2024-04-11 16:36:31'),(30,'liyun','123456','李云',1,'13309090030',NULL,NULL,'30.jpg','2020-03-01','2024-04-11 16:35:33','2024-04-11 16:36:31');
2.3.3 基本查詢
在基本查詢的DQL語句中,不帶任何的查詢條件。
語法如下:
-
查詢多個字段
select 字段1, 字段2, 字段3 from 表名;
-
查詢所有字段(通配符)
select * from 表名;
-
設置別名
select 字段1 [ as 別名1 ] , 字段2 [ as 別名2 ] from 表名;
-
去除重復記錄
select distinct 字段列表 from 表名;
案例演示:
-
案例1:查詢指定字段 name,entry_date并返回
select name,entry_date from emp;
-
案例2:查詢返回所有字段
select * from emp;
* 號代表查詢所有字段,在實際開發(fā)中盡量少用(不直觀、影響效率)
-
案例3:查詢所有員工的 name, entry_date,并起別名(姓名、入職日期)
-- 方式1:
select name AS 姓名, entry_date AS 入職日期 from emp;-- 方式2: 別名中有特殊字符時,使用''或""包含
select name AS '姓 名', entry_date AS '入職日期' from emp;-- 方式3:
select name AS "姓名", entry_date AS "入職日期" from emp;
-
案例4:查詢已有的員工關聯(lián)了哪幾種職位(不要重復)
select distinct job from emp;
2.3.4 條件查詢
語法:
select 字段列表 from 表名 where 條件列表 ; -- 條件列表:意味著可以有多個條件
學習條件查詢就是學習條件的構建方式,而在SQL語句當中構造條件的運算符分為兩類:
-
比較運算符
-
邏輯運算符
常用的比較運算符如下:
常用的邏輯運算符如下:
-
案例1:查詢 姓名 為 '宋江' 的員工
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where name = '宋江'; -- 字符串使用''或""包含
-
案例2:查詢 薪資小于等于 5000 的員工信息
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where salary <=5000;
-
案例3:查詢 沒有分配職位 的員工信息
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where job is null ;
?注意:查詢?yōu)镹ULL的數(shù)據(jù)時,不能使用
= null
或!=null
。得使用is null
或is not null
。
-
案例4:查詢 有職位 的員工信息
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where job is not null ;
-
案例5:查詢 密碼不等于 '123456' 的員工信息
-- 方式1:
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where password <> '123456';
-- 方式2:
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where password != '123456';
-
案例6:查詢 入職日期 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之間的員工信息
-- 方式1:
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where entry_date >= '2000-01-01' and entry_date <= '2010-01-01';-- 方式2: between...and
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where entry_date between '2000-01-01' and '2010-01-01';
-
案例7:查詢 入職時間 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之間 且 性別為女 的員工信息
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where entry_date between '2000-01-01' and '2010-01-01';and gender = 2;
-
案例8:查詢 職位是 2 (講師), 3 (學工主管), 4 (教研主管) 的員工信息
-- 方式1:使用or連接多個條件
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where job=2 or job=3 or job=4;-- 方式2:in關鍵字
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where job in (2,3,4);
-
案例9:查詢 姓名 為兩個字的員工信息
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where name like '__'; # 通配符 "_" 代表任意1個字符
-
案例10:查詢 姓 '張' 的員工信息
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where name like '張%'; # 通配符 "%" 代表任意個字符(0個 ~ 多個)
-
案例11:查詢 姓名中包含 '二' 的員工信息
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
where name like '%二%'; # 通配符 "%" 代表任意個字符(0個 ~ 多個)
2.3.5 聚合函數(shù)
之前我們做的查詢都是橫向查詢,就是根據(jù)條件一行一行的進行判斷,而使用聚合函數(shù)查詢就是縱向查詢,它是對一列的值進行計算,然后返回一個結果值。(將一列數(shù)據(jù)作為一個整體,進行縱向計算)
常用聚合函數(shù):
注意 : 聚合函數(shù)會忽略空值,對NULL值不作為統(tǒng)計。
-
count :按照列去統(tǒng)計有多少行數(shù)據(jù)。
-
在根據(jù)指定的列統(tǒng)計的時候,如果這一列中有null的行,該行不會被統(tǒng)計在其中。
-
-
sum :計算指定列的數(shù)值和,如果不是數(shù)值類型,那么計算結果為0
-
max :計算指定列的最大值
-
min :計算指定列的最小值
-
avg :計算指定列的平均值
案例演示:
-
案例1:統(tǒng)計該企業(yè)員工數(shù)量
-- count(字段)
select count(id) from emp;-- 結果:30
select count(job) from emp;-- 結果:29 (聚合函數(shù)對NULL值不做計算)-- count(常量)
select count(0) from emp;
select count('A') from emp;-- count(*) 推薦此寫法(MySQL底層進行了優(yōu)化)
select count(*) from emp;
-
案例2:統(tǒng)計該企業(yè)員工的平均薪資
select avg(salary) from emp;
-
案例3:統(tǒng)計該企業(yè)員工的最低薪資
select min(salary) from emp;
-
案例4:統(tǒng)計該企業(yè)員工的最高薪資
select max(salary) from emp;
-
案例5:統(tǒng)計該企業(yè)每月要給員工發(fā)放的薪資總額(薪資之和)
select sum(salary) from emp;
2.3.6 分組查詢
-
分組: 按照某一列或者某幾列,把相同的數(shù)據(jù)進行合并輸出。
-
分組其實就是按列進行分類(指定列下相同的數(shù)據(jù)歸為一類),然后可以對分類完的數(shù)據(jù)進行合并計算。
-
分組查詢通常會使用聚合函數(shù)進行計算。
-
語法:
select 字段列表 from 表名 [where 條件] group by 分組字段名 [having 分組后過濾條件];
案例演示:
-
案例1:根據(jù)性別分組 , 統(tǒng)計男性和女性員工的數(shù)量
select gender, count(*)
from emp
group by gender; -- 按照gender字段進行分組(gender字段下相同的數(shù)據(jù)歸為一組)
-
案例2:查詢?nèi)肼殨r間在 '2015-01-01' (包含) 以前的員工 , 并對結果根據(jù)職位分組 , 獲取員工數(shù)量大于等于2的職位
select job, count(*)
from emp
where entry_date <= '2015-01-01' -- 分組前條件
group by job -- 按照job字段分組
having count(*) >= 2; -- 分組后條件
注意事項:
分組之后,查詢的字段一般為聚合函數(shù)和分組字段,查詢其他字段無任何意義
執(zhí)行順序:where > 聚合函數(shù) > having
where與having區(qū)別(面試題)
執(zhí)行時機不同:where是分組之前進行過濾,不滿足where條件,不參與分組;而having是分組之后對結果進行過濾。
判斷條件不同:where不能對聚合函數(shù)進行判斷,而having可以。
2.3.7 排序查詢
排序在日常開發(fā)中是非常常見的一個操作,有升序排序,也有降序排序。
語法:
select 字段列表
from 表名
[where 條件列表]
[group by 分組字段 ]
order by 字段1 排序方式1 , 字段2 排序方式2 … ;
-
排序方式:
-
ASC :升序(默認值)
-
DESC:降序
-
案例演示:
-
案例1:根據(jù)入職時間, 對員工進行升序排序
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
order by entry_date ASC; -- 按照entrydate字段下的數(shù)據(jù)進行升序排序select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
order by entry_date; -- 默認就是ASC(升序)
注意事項:如果是升序, 可以不指定排序方式ASC
-
案例2:根據(jù)入職時間,對員工進行降序排序
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
order by entry_date DESC; -- 按照entrydate字段下的數(shù)據(jù)進行降序排序
-
案例3:根據(jù)入職時間對公司的員工進行升序排序,入職時間相同,再按照更新時間進行降序排序
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
order by entry_date ASC , update_time DESC;
注意事項:如果是多字段排序,當?shù)谝粋€字段值相同時,才會根據(jù)第二個字段進行排序
2.3.8 分頁查詢
分頁操作在業(yè)務系統(tǒng)開發(fā)時,也是非常常見的一個功能,日常我們在網(wǎng)站中看到的各種各樣的分頁條,后臺也都需要借助于數(shù)據(jù)庫的分頁操作。
分頁查詢語法:
select 字段列表 from 表名 limit 起始索引, 查詢記錄數(shù) ;
?
-
案例1:從起始索引0開始查詢員工數(shù)據(jù), 每頁展示5條記錄
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
limit 0 , 5; -- 從索引0開始,向后取5條記錄
-
案例2:查詢 第1頁 員工數(shù)據(jù), 每頁展示5條記錄
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
limit 5 , 5; -- 從索引5開始,向后取5條記錄
-
案例3:查詢 第2頁 員工數(shù)據(jù), 每頁展示5條記錄
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
limit 5 , 5; -- 從索引5開始,向后取5條記錄
-
案例4:查詢 第3頁 員工數(shù)據(jù), 每頁展示5條記錄
select id, username, password, name, gender, phone, salary, job, image, entry_date, create_time, update_time
from emp
limit 10 , 5; -- 從索引10開始,向后取5條記錄
注意事項:
起始索引從0開始。 計算公式 :起始索引 = (查詢頁碼 - 1)* 每頁顯示記錄數(shù)
分頁查詢是數(shù)據(jù)庫的方言,不同的數(shù)據(jù)庫有不同的實現(xiàn),MySQL中是LIMIT
如果查詢的是第一頁數(shù)據(jù),起始索引可以省略,直接簡寫為 limit 條數(shù)