Database數(shù)據(jù)庫
一、課程目錄
理解數(shù)據(jù)庫和表的概念
掌握創(chuàng)建數(shù)據(jù)庫和表的SQL命令
數(shù)據(jù)庫表字段類型、主鍵、備注/ not null
掌握忘表插入數(shù)據(jù)的SQL命令 --- insert
掌握修改表數(shù)據(jù)的SQL命令--- update
掌握刪除數(shù)據(jù)庫、表、數(shù)據(jù)的SQL命令 --- drop、delete
數(shù)據(jù)庫和表的概念
思考:
物業(yè)網(wǎng)站展示的信息 是存放在哪里?
數(shù)據(jù)庫的概念:
存儲數(shù)據(jù)的容器( 可以理解為 倉庫 )
表的概念:
一對多的關(guān)系,一個庫里通常維護多個表(貨架)
一個數(shù)據(jù)庫服務(wù)器下面,多個庫
創(chuàng)建數(shù)據(jù)庫
語法:
create database 數(shù)據(jù)庫名稱
例如:創(chuàng)建數(shù)據(jù)庫
create database test_db ;
創(chuàng)建表
思考:如何將 Excel表的數(shù)據(jù) 保存到 數(shù)據(jù)庫中的某個表里面
分析:為了將數(shù)據(jù)放在表里,需要創(chuàng)建的表,必須跟Excel的數(shù)據(jù)庫結(jié)果一樣
語法
create table 表名 ( 字段名1 類型1,字段名2 類型2 ... ) Default CharSet=utf8 ;
解釋
字段名:用來標識表的一列
字段類型:用來聲明每一個字段存儲數(shù)據(jù)的類型,因為在mysql中,數(shù)據(jù)有類型(整數(shù)、小數(shù)、字符、時間 ... )
字符集:UTF8,用來指定表支持中文數(shù)據(jù)存儲
例如:創(chuàng)建 student表的SQL
create table student(
id int not null Primary key auto_increment,
name varchar(12),
sex varchar(12),
class_id int(11),
address varchar(12),
create_date datetime,
update_date datetime
) Default Charset=utf8 ;
常見字段類型
每種字段都有自己的類型,如:整數(shù)型、字符型、時間類型、日期類型、浮點型 等
常用的數(shù)據(jù)類型:
1)整型 -- 整數(shù)
tinyint:占1個字節(jié),取值范圍: -128 ~ 127
int :占4個字節(jié),取值范圍: -214748364 ~ 2147483647
2)字符串型
char(n): n個字符,最多 255 個字符,固定長度
varchar(n):n個字符,最多 65535個字符,可變長度
3)時間和日期
date: 日期,格式(yyy-mm-dd,例如: 2016-08-22)
time: 時間,格式(hh:mm:ss ,例如: 18:40:37 )
datetime: 日期時間,格式(yyy-mm-dd hh:mm:ss,例如:2016-08-22 18:41:27)
4) 浮點型(小數(shù))
float(m,d): 單精度浮點型,32bit,m代表總位數(shù),d小數(shù)位;如99.9聲明類型為:float(4,2)
double(m,d):雙精度浮點型,64bit,m代表總位數(shù),d小數(shù)位
decimal(m,d):m代表總數(shù)位,d小數(shù)位;decimal數(shù)據(jù)類型用于精度要求非常高的計算中,
比如語句 decimal(7,3) 規(guī)定存儲的數(shù)值不能超過7位數(shù)字,并且小數(shù)點后不能超過3位
主鍵
主鍵是什么? 它有什么特征
主鍵字段存儲的數(shù)值不能重復(fù),因此 主鍵可以唯一標識一行數(shù)據(jù)
一個表只有一個主鍵
主鍵是非必需的,也就是說一個表可以不聲明主鍵字段
語法
<字段名><類型> primary key ;
思考: 上面 student 表那個字段適合做主鍵
主鍵數(shù)值 生成策略
自動增長 auto_increment
備注 comment '性別,1-男,2-女,3-其他'
小結(jié)
1、創(chuàng)建表聲明字段時候,字段之間以什么符號分隔?符號用中文的可以嗎? 英文輸入法 -- 英文符號 ; 不能
2、建表聲明字段的時候,最后一個字段后面需注意什么?
不能留 "," 逗號
3、主鍵的特征是什么?
唯一性、非空性 ; primary key 聲明;
4、如果表需要支持中文數(shù)據(jù)存儲,需要怎么做?
Default Charset=utf8 ; 設(shè)置字符集
5、創(chuàng)建表了,發(fā)現(xiàn)表沒有出現(xiàn),可以刷新一下表。F5刷新
6、Navicat 里 SQL寫完丟失了,其實是因為沒有保存。Ctrl+S 保存
7、學(xué)會分析錯誤:near附近+line行數(shù) 排查問題
插入表數(shù)據(jù)(insert)
插入一條數(shù)據(jù),對所有字段都賦值
語法
1)insert into <表名> values ( 數(shù)值A(chǔ),數(shù)值B ... );
2)insert into <表名>( 字段1,字段2 ...) values( 數(shù)值A(chǔ),數(shù)值B ... );
例如:
1)insert into t_user values( 1,'tommy','123','男');
2)insert into tb_user(id,name,age,sex,tel,score) values(1,'zsf',18,'1','17722857587',90);
插入一條數(shù)據(jù),并且對一部分字段賦值
語法
insert into <表名>(字段A,字段B ...) values ( 值A(chǔ),值B ...);
例如
insert into t_user( user_name,Pwd,Phone,Type) values( 'nick','123','蘋果',5 );
總結(jié)
如果插入數(shù)據(jù)時,只對部分字段進行初始化,則需聲明要插入的字段
插入表數(shù)據(jù)( insert )
批量插入,一次插入多條數(shù)據(jù)
語法
insert into <表名> values ( 值1,值2 ... ),( 值1,值2 ... );
例如:
insert into t_user (user_name,Pwd,Phone,Type)
values ( 'nick','123','蘋果',5 ),
( 'nick','456','vivo',8 ),
( 'nick','789','華為',5 );
修改表數(shù)據(jù)(update)
語法
update 表名 set 字段名1 = 新值1,字段名2 = 新值2 ...
例如:將用戶表(t_user) 里所有用戶類型的type修改為4
update t_user set type = 4 ;
修改一個表里滿足某些條件的那一部分數(shù)據(jù)
語法
update 表名 set 字段名1 = 新值1,字段名2 = 新值2 ... where 條件表達式1 and(or) 條件表達式2 ... ;
例如:
將用戶表(t_user)里的 'nick'用戶的type修改為 4
update t_user set type = 4 where user_name = 'nick';
總結(jié):
如果要更新一個表滿足某個條件的數(shù)據(jù),需用到關(guān)鍵字 where 來進行過濾找出 那一部分數(shù)據(jù)。然后針對這一部分數(shù)據(jù)做更新操作
刪除數(shù)據(jù)庫 或 表( drop )
刪除數(shù)據(jù)庫
語法
drop database 數(shù)據(jù)庫名稱 ;
例如:刪除數(shù)據(jù)庫 test_db
drop database test_db ;
刪除數(shù)據(jù)表
語法
drop table 表名 ;
例如:刪除表 test_table
drop table test_table ;
總結(jié)
drop database A ;會把數(shù)據(jù)庫A刪除,因此庫里的所有表和數(shù)據(jù)全部刪除
drop table B ;會把表B刪除,因此表B的數(shù)據(jù)也會刪除
drop 作為一種刪除命令,它不僅僅刪除數(shù)據(jù)結(jié)果(庫/表),并且刪除數(shù)據(jù)
簡單來講:drop 刪除結(jié)構(gòu)和數(shù)據(jù),所有這兩個命令要慎用
刪除表數(shù)據(jù)(delete)
刪掉表里所有數(shù)據(jù)
語法
delete from 表名 ;
例如:刪除用戶表里所有數(shù)據(jù)
delete from t_user ;
刪除一個表里滿足條件的數(shù)據(jù)
語法
delete from 表名 where 條件1 and(or) 條件2 ... ;
例如:刪除會員表名為 'tommy'的數(shù)據(jù)
delete from user where user_name = 'tommy' ;
總結(jié):
1)如果要刪除某個滿足某條件的數(shù)據(jù),需要用到關(guān)鍵字 where來進行過濾找出那一部分數(shù)據(jù)
2)delete 也是一種刪除命令,但是與drop命令不同的是,delete只會刪除表里面的數(shù)據(jù),不會刪除表和庫。簡單來說,delete刪除數(shù)據(jù)不刪結(jié)果
小結(jié)
1、插入數(shù)據(jù)時,只初始化部分字段與初始化所有字段時 SQL的不同體現(xiàn)在哪里?
聲明字段
2、賦值時,對于varchar類型的字段,賦值時應(yīng)注意什么?
加引號
3、字段聲明了 長度,賦值時應(yīng)該注意什么?插入數(shù)據(jù)的長度 超長了有問題嗎?
報錯 / 截斷
4、插入一條數(shù)據(jù)和多條數(shù)據(jù)的語法有區(qū)別是? 多條數(shù)據(jù)時,中間以什么符號分隔?
1)插入單條數(shù)據(jù)
insert into persons
(id_p, lastname , firstName, city )
values
(204,'haha' , 'deng' , 'shenzhen');
2)插入多條數(shù)據(jù)
insert into persons
(id_p, lastname , firstName, city )
values
(200,'haha' , 'deng' , 'shenzhen'),
(201,'haha2' , 'deng' , 'GD'),
(202,'haha3' , 'deng' , 'Beijing');
區(qū)別:values 后面 按插入數(shù)據(jù)格式 直接寫多條數(shù)據(jù)
分隔:以 ','逗號分隔
練習(xí)
[圖片上傳失敗...(image-a08ce0-1673241167164)]
[圖片上傳失敗...(image-ed0c50-1673241167164)]
[圖片上傳失敗...(image-dba859-1673241167164)]
[圖片上傳失敗...(image-cda021-1673241167164)]
[圖片上傳失敗...(image-2d7b30-1673241167164)]
[圖片上傳失敗...(image-c94b4e-1673241167164)]
作業(yè)一:
舉例:后臺注冊操作,哪些表有數(shù)據(jù)變化,具體哪些變化? --增刪改查
--t_user表會新增一條數(shù)據(jù),且type為0
1、后臺添加業(yè)主信息,哪些表有數(shù)據(jù)變化,具體哪些變化?
--t_owner表新增一條數(shù)據(jù),status 為 2
2、后臺添加單元信息,哪些表有數(shù)據(jù)變化,具體哪些變化?
--t_unit表新增一條記錄
3、完善代理商角色的賬號信息,哪些表有數(shù)據(jù)變化,具體哪些變化?
--t_user表的 type為2
--merchant表新增一條數(shù)據(jù),且merchant_type為2
4、后臺添加一個小區(qū),且審核通過了,哪些表有數(shù)據(jù)變化,具體哪些變化?
--t_community新增一條數(shù)據(jù),審核通過后修改state為1
5、綁定業(yè)主,哪些表有數(shù)據(jù)變化,具體哪些變化?
--t_owner_user新增一條數(shù)據(jù)
--t_user 新增一條數(shù)據(jù),且 type 為 5
新增次數(shù) 和 獲取短信驗證碼次數(shù)有關(guān)
作業(yè)二:
| id | user_name | pwd | phone | type | reg_time |
|---|---|---|---|---|---|
| 1001 | Jack | 123456 | 78813989123 | 1 | 2017-01-01 13:07:08 |
| 1002 | Rose | 123456 | 78813989123 | 1 | 2017-01-01 13:07:08 |
| 1103 | Tommy | 123456 | 78813989123 | 1 | 2017-01-01 13:07:08 |
| 1004 | Nick | 123456 | 78813989123 | 1 | 2017-01-01 13:07:08 |
| 1005 | Peter | 123456 | 78813989123 | 1 | 2017-01-01 13:07:08 |
1、新增用戶表(t_user)數(shù)據(jù),按照截圖里給出的對應(yīng)的表數(shù)據(jù),使用insert語句往對應(yīng)的表里插入對應(yīng)的數(shù)據(jù)
#單行插入
#insert into t_user values ( 1001,'Jack','123456','78813989123', '1', '2017-01-01 13:07:08') ;
#多行插入
insert into t_user values ( 1002,'Rose','123456','78813989123', '1', '2017-01-01 13:07:08') ,
( 1003,'Tommy','123456','78813989123', '1', '2017-01-01 13:07:08'),
( 1004,'Nick','123456','78813989123', '1', '2017-01-01 13:07:08') ,
( 1005,'Peter','123456','78813989123', '1', '2017-01-01 13:07:08') ;
2、使用insert語句在樓棟表(t_building)中新增兩條樓棟信息,要求:
-- 新增一個樓棟id為2001,樓棟編號為567,樓名稱為"德智樓",建筑面試10000,小區(qū)id為9527,創(chuàng)建時間為2021年1月1日的數(shù)據(jù)
-- 新增一個樓棟id為3001,樓棟編號為456,樓名稱為"體美樓",建筑面試345.67,小區(qū)id為9527,創(chuàng)建時間為2021年1月1日的數(shù)據(jù)
1)單行插入
insert into t_building( building_id,building_num,building_name,floorage,community_id,create_time)
values( 2001 ,'567','德智樓',10000, 9527, '2021-01-01' );
2)多行插入
insert into t_building( building_id,building_num,building_name,floorage,community_id,create_time)
values( 2002 ,'567','德智樓',10000, 9527, '2021-01-01' ) , ( 3001 ,'456','體美樓',345.67, 9527, '2021-01-01' );
3、SQL更新用戶Nick的手機號為 13112341234
update t_user set phone='13112341234' where user_name='Nick' ;
4、使用SQL刪除用戶表(t_user) Peter 的會員信息
delete from t_user where user_name='Peter';
二、本課目標
掌握數(shù)據(jù)庫查詢的SQL命令 select
單表查詢
多表查詢
左右連接查詢
單表查詢
不加條件的查詢
1)查詢特定字段,語法
select <字段1,字段2 ... > from < 表名 > ;
例如:查看會員表的所有用戶的手機號碼和用戶類型
select phone,type from t_user ;
2)查看所有字段,語法
select * from < 表名 > ;
例如:查看會員表的所有用戶信息
select * from t_user ;
單表查詢
按條件進行查詢
1)多個條件需同時成立,語法
select ... from <表名> where 表達式Aand 表達式B and ... 表達式N ;
例如:查看會員表中類型為:物業(yè)、且注冊時間大于 2021年4月21日
select * from t_user where type=3 and reg_time > '2021-04-21';
2)多個條件不需同時成立,只需滿足其中一個或多個,語法
select ... from 表名 where 字段a = 值a or 字段b = 值b or 字段c = 值c ;
例如:查看會員表中用戶ID為1或ID為2或用戶名為 Nick 的用戶信息
select * from t_user where id = 1 or id = 2 or user_name = 'Nick' ;
多表查詢
問題一
1、對于不同類型的信息怎么存儲?是放一個表 還是不同類型數(shù)據(jù)放在 不同表
2、放在一個表的缺點
字段過多
難以維護
表數(shù)據(jù)龐大
數(shù)據(jù)冗余,重復(fù)數(shù)據(jù)過多
問題二
1、既然不同類型的數(shù)據(jù)放在不同表,那原來有關(guān)聯(lián)的數(shù)據(jù)怎么保持原有的關(guān)系呢?
解決方案:
設(shè)計表的時候兩個表之間維持一個關(guān)聯(lián)即可。
---建表,設(shè)立關(guān)聯(lián)字段(id)
t_user表中 id 字段 與 t_merchant 表 的用戶編號 userid 構(gòu)成一個關(guān)聯(lián):
t_user.id = t_merchant.user_id
多表查詢
語法
select 要查詢字段 from 表1,表2 ... where 關(guān)聯(lián)條件和過濾條件
例如:
查詢 id為12的用戶名、手機號、創(chuàng)建時間、該用戶商戶編號、商戶名稱、成立日期
分析:
涉及表:用戶表 t_user、商戶表t_merchant
查詢字段:
會員表(user_name,phone,reg_time),商戶表(id,merchant_name,establish_data)
關(guān)聯(lián)關(guān)系:
t_user.id = t_merchant.user_id
過濾條件:
t_user.id = 12
連接查詢
| 語法 | ||
|---|---|---|
| Inner Join | 內(nèi)連接 | 又稱為 等值連接,獲取兩個表中字段匹配關(guān)系的記錄 |
| Left Join | 左連接 | 讀取左邊表的全部數(shù)據(jù),即使右邊表沒有關(guān)聯(lián)數(shù)據(jù),左連接會從左表產(chǎn)生一套完整的記錄,和右表匹配記錄。如果右表每一匹配記錄,則右表結(jié)果集字段為 null |
| Right Join | 右連接 | 以右表為基礎(chǔ),與 Left Join 相反 |
[圖片上傳失敗...(image-e3ed8a-1673241167164)]
案例
[圖片上傳失敗...(image-2a4884-1673241167164)]
查詢結(jié)果排序(order by)
對結(jié)果集進行排序
語法:
select ... from ... order by 字段A asc( desc );
select ... from ... order by 字段A asc( desc ),字段A desc ;
例如:查詢用戶表所有普通用戶信息,按可用余額升序、降序排列
select * from t_user where type = 1 order by reg_time ;
select * from t_user where type = 1 order by reg_time desc ;
注意:
如果不寫排序 規(guī)則,默認排序為 asc ;
1、升序 asc
2、降序 desc
練習(xí)
1、建表與數(shù)據(jù)
為管理業(yè)務(wù)培訓(xùn)信息,建立3個表
1、S( S# , SN ,SD,sA )S#,SN, SD,SA分別代表學(xué)號,學(xué)員姓名,所屬單位,學(xué)員年齡
2、C( C# , CN )C#,CN 分別代表課程編號,課程名稱
3、SCC( S# , C# , G )S#,C#,G分別代表學(xué)號,所選的課程編號,學(xué)習(xí)成績
-- ----------------------------
-- Table structure for `S`
-- ----------------------------
CREATE TABLE `S` (
`S#` varchar(255) DEFAULT NULL,
`SN` varchar(255) DEFAULT NULL,
`SD` varchar(255) DEFAULT NULL,
`SA` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of S
-- ----------------------------
INSERT INTO `S` VALUES ('1', '張三', '銷售部', '24');
INSERT INTO `S` VALUES ('2', '李四', '銷售部', '30');
INSERT INTO `S` VALUES ('3', '王五', '運營部', '28');
INSERT INTO `S` VALUES ('4', '趙六', '運營部', '27');
INSERT INTO `S` VALUES ('5', '孫七', '運營部', '23');
INSERT INTO `S` VALUES ('6', '周八', '開發(fā)部', '33');
INSERT INTO `S` VALUES ('7', '吳九', '開發(fā)部', '29');
-- ----------------------------
-- Table structure for `C`
-- ----------------------------
CREATE TABLE `C` (
`C#` varchar(255) DEFAULT NULL,
`CN` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of C
-- ----------------------------
INSERT INTO `C` VALUES ('C1', '稅收基礎(chǔ)');
INSERT INTO `C` VALUES ('C2', '開發(fā)大全');
INSERT INTO `C` VALUES ('C3', '運營之道');
INSERT INTO `C` VALUES ('C4', '銷售之王');
-- ----------------------------
-- Table structure for `SCC`
-- ----------------------------
CREATE TABLE `SCC` (
`S#` varchar(255) DEFAULT NULL,
`C#` varchar(255) DEFAULT NULL,
`G` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of SCC
-- ----------------------------
INSERT INTO `SCC` VALUES ('1', 'C4', '80');
INSERT INTO `SCC` VALUES ('2', 'C4', '70');
INSERT INTO `SCC` VALUES ('3', 'C1', '90');
INSERT INTO `SCC` VALUES ('4', 'C3', '66');
INSERT INTO `SCC` VALUES ('5', 'C3', '81');
INSERT INTO `SCC` VALUES ('6', 'C2', '91');
INSERT INTO `SCC` VALUES ('7', 'C2', '56');
1.查詢選修課程名稱為'稅收基礎(chǔ)'的學(xué)員學(xué)號和姓名?
分析:
1、學(xué)員學(xué)號和姓名 --S 、課程名稱 --C
2、多表 ---關(guān)聯(lián)條件: S.S# and C.C# = SCC.C#
3、過濾條件 C.CN = '稅收基礎(chǔ)'
4、查詢字段寫完整
1)where查詢
select S.`S#`,S.SN from S,C,SCC where SCC.`S#` = S.`S#` and C.`C#` = SCC.`C#` and C.CN = '稅收基礎(chǔ)';
2)聯(lián)合查詢
select S.`S#`,S.SN from S where S.`S#` = ( SELECT SCC.`S#` from SCC INNER JOIN C where C.CN='稅收基礎(chǔ)' and SCC.`C#`=C.`C#` ) ;
2.查詢選修課程編號為'C2'的學(xué)員姓名和所屬單位?
分析:
1、姓名和所屬單位 --S 、課程編號 --SCC
2、兩表關(guān)聯(lián)條件 S.S# = SCC.S#
3、過濾條件 SCC.C# = 'C2'
4、補齊查詢字段
1)子查詢
select S.SN,S.SD from S,SCC where S.`S#` = SCC.`S#` and SCC.`C#` = 'C2' ;
三、本節(jié)課目標
掌握 in 的用法
掌握模糊查詢 like 的用法
掌握統(tǒng)計 count() 的用法
掌握分組 group by 的用法
掌握去重 distinct 的用法
掌握 between 的用法
掌握 mysql 的分頁
掌握 mysql 常用函數(shù)
查詢 in 用法
使用場景:查詢時候,條件字段的值存在于某個數(shù)據(jù)集
語法:
select ... from 表名 where 條件字段 in (數(shù)據(jù)集)
數(shù)據(jù)集可以是具體的某個值:值a,值b,值n;也可以是通過一個子查詢得到的數(shù)據(jù)集
例如:
查詢用戶 id 為 1,2, 3 的用戶信息
select * from t_user where id in (1,2,3);
拓展
not in 的用法:條件字段的值不存在于某個數(shù)據(jù)集
查詢用戶 id 不為 1,2,3的用戶
select * from t_user where id not in (1,2,3);
模糊查詢 - like
使用場景:查詢的時候,條件字段的值不完整的情況
語法:
%百分號來匹配任意長度的字符串,而且%出現(xiàn)的位置不同所代表的意義不同
條件字段的值 以任意字符串開頭,以 xx 結(jié)尾的值
select <字段1,字段2, ... > from <表名> where <條件字段> like 'xx%' ;
條件字段的值 包含 xx
select <字段1,字段2, ... > from < 表名 > where <條件字段 > like '%xx%' ;
分組 - group by
使用場景:
group by 顧名思義就是按照某一個,或 多個 字段來分組。一般通過 "聚合函數(shù)" 配合使用,使用時 至少需要一個分組字段。某某信息來進行分組
語法
select 查詢字段,聚合函數(shù) from 查詢涉及的表 group by 分組字段 having 過濾條件 ;
語法解析
1)聚合函數(shù):對一組數(shù)值執(zhí)行計算并返回的函數(shù)。聚合函數(shù)經(jīng)常與 select 語句 的 group by 子句 一同使用,常見聚合函數(shù)有: sum()、count()、avg()、min()、max() 等
2)having: 在分組以后如果想在這個分組結(jié)果的基礎(chǔ)上繼續(xù)過濾的話,必須把過濾條件寫到 having后面
例如:
請按小區(qū)分組,統(tǒng)計樓棟表中 各個小區(qū)的 樓棟數(shù)
select community_id ,count( building_id ) '樓棟編號' from t_building group by community_id ;
數(shù)值相關(guān)函數(shù)
1、求字段A的最小值: min(字段A)
2、求字段A的最大值: max(字段A)
3、求字段A的平均值: avg(字段A)
4、求字段A的和: sum(字段A)
1)日期函數(shù)
獲取系統(tǒng)當(dāng)前日期時間: sysdate()
獲取系統(tǒng)當(dāng)前日期: curdate()
獲取系統(tǒng)當(dāng)前時間: curtime()
獲取給定日期的年份:year(date)
獲取給定日期的月份:month(date)
為指定日期增加一個時間間隔的函數(shù):date_add( date,interval expr unit )
2)字符串函數(shù):
字符串拼接函數(shù): concat( 字段A,字段B )
字符串截取函數(shù): substr( 字段A,截取開始位置 position,截取字符串個數(shù) )
獲取字符串的函數(shù):length(字段A)
大小寫字母轉(zhuǎn)換的函數(shù): lower()、upper()
查詢 -between
使用場景
條件字段的取值處于兩個字段范圍內(nèi)的情況
語法
select ... from 表名 where 條件字段 between 數(shù)值A(chǔ) and 數(shù)值B ;
例如:
找出用戶表里 注冊時間為 2021年4月10號至 20號的注冊用戶信息( 包含邊界值 )
select * from t_user where reg_time between '2021-04-01 00:00:00' and '2021-04-20 23:59:09' ;
查詢 - 去重(distinct)
使用場景:
去除 查詢結(jié)果中的重復(fù)數(shù)據(jù)
語法
select distinct 字段1 from 表名 ;
例如:
查詢所有注冊的賬戶角色
select distinct type from t_user ;
查詢 - 分頁( limit )
使用場景
取查結(jié)果的前 n 條記錄
語法
select ... from 表名 limit m,n ;
m:指的索引值是從m開始 ; n: 表述每頁要去多少條數(shù)據(jù)
m = ( 第幾頁的頁數(shù) - 1) * 每頁條數(shù) ; n = 每頁條數(shù)
例如:
每頁取 10 條記錄
第一頁: limit 0 ,10 表示 索引從 0 開始 取 10條記錄
第一頁: limit 10,10 表示 索引從 10 開始 取 10條記錄
第一頁: limit 20,10 表示 索引從 20 開始 取 10條記錄
...
思考:
1)如每頁展示 x 條數(shù)據(jù),取第 y頁時,分頁語句中的
m = ( y - 1 ) * x 和 n = x 值分別為多少?
2)請用SQL將 t_user 表數(shù)據(jù)按照 id 降序排列后取 前 3 條數(shù)據(jù)
select 語句查詢結(jié)構(gòu)
select
...
from
...
[where]
...
[group by]
...
[having]
...
[order by]
...
[limit]
...
注意:
進行查詢數(shù)據(jù)時,過濾條件(關(guān)鍵詞) 是有順序的
練習(xí)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `authors`
-- ----------------------------
DROP TABLE IF EXISTS `authors`;
CREATE TABLE `authors` (
`authorId` int(11) NOT NULL,
`authorName` varchar(50) default NULL,
`sex` varchar(2) default NULL,
`age` int(11) default NULL,
`city` varchar(50) default NULL,
`telephone` varchar(11) default NULL,
`sales` int(11) default NULL,
`isbn` datetime default NULL,
`create_date` datetime default NULL,
`update_date` datetime default NULL,
PRIMARY KEY (`authorId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of authors
-- ----------------------------
INSERT INTO `authors` VALUES ('1', '小趙', '男', '18', '西安', '18829346424', '34', '2016-01-09 10:22:37', '2016-10-09 10:22:37', '2016-10-09 10:22:37');
INSERT INTO `authors` VALUES ('2', '小錢', '女', '19', '重慶', '18329346888', '35', '2016-02-09 10:22:37', '2016-10-09 10:22:37', '2016-10-09 10:22:37');
INSERT INTO `authors` VALUES ('3', '小孫', '男', '20', '上海', '15882328888', '36', '2015-03-09 10:22:37', '2016-10-09 10:22:37', '2016-10-09 10:22:37');
INSERT INTO `authors` VALUES ('4', '張力', '男', '51', '四川', '13459346424', '38', '2017-04-09 10:22:37', '2016-10-09 10:22:37', '2016-10-09 10:22:37');
INSERT INTO `authors` VALUES ('5', '小李', '男', '25', '西安', '18929346424', '39', '2016-05-09 10:22:37', '2016-10-09 10:22:37', '2016-10-09 10:22:37');
INSERT INTO `authors` VALUES ('6', '小周', '女', '36', '懷化', '15537458345', '34', '2016-06-09 10:22:37', '2016-10-09 10:22:37', '2016-10-09 10:22:37');
INSERT INTO `authors` VALUES ('7', '小吳', '女', '58', '合陽', '17323456394', '35', '2017-07-09 10:22:37', '2016-10-09 10:22:37', '2016-10-09 10:22:37');
INSERT INTO `authors` VALUES ('8', '小王', '女', '48', '珠海', '18829346424', '37', '2016-08-09 10:22:37', '2016-10-09 10:22:37', '2016-10-09 10:22:37');
作者表 : authors
作者編號: authorId int(11)
作者姓名: authorName varchar(50)
性別: sex varchar(2)
年齡: age int
居住城市: city varchar(50)
聯(lián)系電話: telephone varchar(11)
銷量: sales int(11)
最新出版日期:isbn datetime
1)查詢 姓 張 的作者信息
select * from authors where authorName like '張%%' ;
2)查詢聯(lián)系電話第三位為8或9并以888結(jié)尾的作者信息
select * from authors where substr(telephone,3,1) in ('8','9') and telephone like '%888' ;
3)查詢顯示作者的姓名,銷量,并按銷量降序排列
select authorName,sales from authors order by sales desc ;
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL,
`cname` varchar(12) default NULL,
`tid` int(11) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('1', '語文', '1001');
INSERT INTO `course` VALUES ('2', '數(shù)學(xué)', '1002');
INSERT INTO `course` VALUES ('3', '外語', '1003');
-- ----------------------------
-- Table structure for `sc`
-- ----------------------------
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc` (
`sid` int(11) default NULL,
`cid` int(11) default NULL,
`score` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of sc
-- ----------------------------
INSERT INTO `sc` VALUES ('16001', '1', '55');
INSERT INTO `sc` VALUES ('16001', '2', '40');
INSERT INTO `sc` VALUES ('16002', '1', '51');
INSERT INTO `sc` VALUES ('16002', '3', '80');
INSERT INTO `sc` VALUES ('16003', '1', '40');
INSERT INTO `sc` VALUES ('16003', '2', '55');
INSERT INTO `sc` VALUES ('16003', '3', '50');
INSERT INTO `sc` VALUES ('16004', '1', '99');
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL,
`sname` varchar(10) default NULL,
`sage` int(11) default NULL,
`ssex` varchar(10) default NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('16001', '張三', '21', '男');
INSERT INTO `student` VALUES ('16002', '李四', '21', '男');
INSERT INTO `student` VALUES ('16003', '王五', '21', '男');
INSERT INTO `student` VALUES ('16004', '趙六', '22', '男');
-- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`tid` int(11) default NULL,
`tname` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('1001', '李家俊');
INSERT INTO `teacher` VALUES ('1002', '徐根寶');
INSERT INTO `teacher` VALUES ('1003', '葉平');
student表(sid,sname,sage,ssex)--學(xué)號,姓名,學(xué)生年齡,性別
course表(cid,cname,tid)--課程代碼,課程名稱,老師工號
sc表(sid,cid,score)--學(xué)號,課程代碼,分數(shù)
teacher表(tid,tname)--老師工號,老師姓名
1)查詢平均成績大于60分的學(xué)生學(xué)號和平均成績。
select sid'學(xué)生學(xué)號',avg(score)'平均成績' from sc group by sid having avg(score) > 60 ;
2)查詢所有學(xué)生的學(xué)號,姓名,選課數(shù),總成績。
SELECT student.sid'學(xué)號' , student.sname'姓名', COUNT( student.sid )'選課數(shù)', SUM( sc.score )'總成績'
FROM student INNER JOIN sc ON student.sid = sc.sid
GROUP BY student.sid, student.sname ;
3)查詢沒有學(xué)過葉平老師課程的學(xué)生的姓名和學(xué)號。
select student.sid,student.sname from student where student.sid
not in
(
select sc.sid from sc inner join course on sc.cid=course.cid
inner join teacher on course.tid = teacher.tid where teacher.tname = '葉平'
) ;
4)查詢所有課程成績都小于60分的學(xué)生姓名和學(xué)號。
select sid,sname from student where sid in( select distinct sid from sc where not score > 60 ) ;