SQL語句:結(jié)構(gòu)化查詢語言
- DDL: 數(shù)據(jù)定義語言, 定義數(shù)據(jù)庫對象:庫翅萤、表恐疲、列、表結(jié)構(gòu)
- DML: 數(shù)據(jù)操作語言套么, 定義數(shù)據(jù)庫記錄培己,增、刪胚泌、改 表記錄
- DQL: 數(shù)據(jù)查詢語言省咨, 查詢記錄
- DCL: 數(shù)據(jù)控制語言, 定義訪問權(quán)限和控制安全級別
1. DDL
1.1 數(shù)據(jù)庫
SHOW DATABASES: 查看所有數(shù)據(jù)庫
USE 數(shù)據(jù)庫名:切換到某數(shù)據(jù)庫
CREATE DATABASE [IF NOT EXISTS] 數(shù)據(jù)庫名 [CHARSET=UTF8]
DROP DATABASE [IF EXISTS] 數(shù)據(jù)庫名
1.2 表
SHOW TABLES: 查看數(shù)據(jù)庫下所有表
DESC 表名:查看表結(jié)構(gòu)
CREATE TABLE [IF NOT EXISTS] 表名 (列名 列類型玷室, 列名 列類型零蓉, ......);
DROP TABLE 表名
* 修改表:前綴 ALTER TABLE 表名笤受, alter table 表名 add|change|drop 列名 類型;
1. 添加列: ADD (列名 列類型, 列名 列類型敌蜂, ......)
2. 刪除列: DROP 列名
3. 修改列類型: MODIFY 列名 列類型
4. 修改列名: CHANGE 原列名 新列名 列類型
5. 修改表名: RENAME TO 新表名
2. DML
2.1 插入數(shù)據(jù)
INSERT INTO 表名
2.2 修改數(shù)據(jù)
UPDATE 表名 SET ... [where ...]
2.3 刪除數(shù)據(jù)
DELETE FROM 表名 [where ...]
3. DQL
- 基本查詢
- 條件控制
- 聚合
- 分組
- 排序
- 分頁
3.1 基本查詢
select * from 表名 [where ...]
select 列名1[, 列名2, ...] from 表名 [where ...]
select distinct * from 表名 [where ...] : 去重
select 列名 * 1.5 from 表名 [where ...]: 字符串被當做0
select 列名1 + 列名2 from 表名 [where ...]: NULL與任何運算都為NULL
3.2 條件控制
3.2.1 比較運算符
- 等于: =
- 大于: >
- 大于等于: >=
- 小于: <
- 小于等于: <=
- 不等于: !=或<>
select * from students where id > 3;
select * from subjects where id <= 4;
select * from students where sname != '黃蓉';
select * from students where isdelete = 0;
3.2.2 邏輯運算符
- and
- or
- not
select * from students where id>3 and gender=0;
select * from students where id<4 or isdelete=0;
3.2.3 模糊查詢
- like: 關(guān)鍵字
- % 表示可以匹配任意多個任意字符感论,0,1或多個
- _ 表示可以匹配一個任意字符,1個
select * from students where sname like '黃%'; 查詢姓黃的學生
select * from students where sname like '黃_'; 查詢姓黃并且名字是一個字的學生
select * from students where sname like '黃%' or sname like '%靖%'; 查詢姓黃或叫靖的學生
3.2.4 范圍查詢
- in 表示在一個非連續(xù)的范圍內(nèi)
- between ... and ...表示在一個連續(xù)的范圍內(nèi)
select * from students where id in(1,3,8); 查詢編號是1或3或8的學生
select * from students where id between 3 and 8; 查詢學生是3至8的學生
select * from students where id between 3 and 8 and gender=1; 查詢學生是3至8的男生
3.2.5 空判斷
- null與' '是不同的
- 判空 is null
- 判非空 is not null
select * from students where hometown is null;查詢沒有填寫地址的學生
select * from students where hometown is not null; 查詢填寫了地址的學生
select * from students where hometown is not null and gender=0; 查詢填寫了地址的女生
3.2.6 優(yōu)先級
- 小括號紊册,not比肄,比較運算符,邏輯運算符
- and比or先運算囊陡,如果同時出現(xiàn)并希望先算or芳绩,需要結(jié)合()使用
3.3 聚合
- count(*) 計算總行數(shù)
- max(列)
- min(列)
- sum(列)
- avg(列)
select count(*) from students;查詢學生總數(shù)
select max(id) from students where gender=0;查詢女生的編號最大值
select min(id) from students where isdelete=0;查詢未刪除的學生最小編號
select sum(id) from students where gender=1;查詢男生的編號之和
select avg(id) from students where isdelete=0 and gender=0;查詢未刪除女生的編號平均值
3.4 分組
- group by 列1,列2,列3...
- having 分組后的數(shù)據(jù)篩選
- having后面的條件運算符與where的相同
按照字段分組,表示此字段相同的數(shù)據(jù)會被放到一個組中
分組后撞反,只能查詢出相同的數(shù)據(jù)列妥色,對于有差異的數(shù)據(jù)列無法出現(xiàn)在結(jié)果集中
可以對分組后的數(shù)據(jù)進行統(tǒng)計,做聚合運算
select 列1,列2,聚合... from 表名 group by 列1,列2,列3...
select gender as 性別, count(*) from students group by gender; 查詢男女生總數(shù)
select hometown as 家鄉(xiāng),count(*) from students group by hometown;查詢各城市人數(shù)
select 列1,列2,聚合... from 表名 group by 列1,列2,列3... having 列1,...聚合...
1. select count(*) from students where gender=1; 查詢男生總?cè)藬?shù)
2. select gender as 性別,count(*) from students group by gender having gender=1;
對比where與having:
where是對from后面指定的表進行數(shù)據(jù)篩選遏片,屬于對原始數(shù)據(jù)的篩選
having是對group by的結(jié)果進行篩選
3.6 排序
- order by
- desc | asc
將行數(shù)據(jù)按照列1進行排序嘹害,如果某些行列1的值相同時,則按照列2排序吮便,以此類推
默認按照列值從小到大排列
asc從小到大排列笔呀,即升序
desc從大到小排序,即降序
select * from 表名 order by 列1 asc|desc, 列2 asc|desc,...
select * from students where gender=1 and isdelete=0 order by id desc;查詢未刪除男生學生信息髓需,按學號降序
select * from subject where isdelete=0 order by stitle;查詢未刪除科目信息许师,按名稱升序
3.7 分頁
- limit start,count
- 從start開始,獲取count條數(shù)據(jù)
- start索引從0開始
當數(shù)據(jù)量過大時僚匆,在一頁中查看數(shù)據(jù)是一件非常麻煩的事情
已知:每頁顯示m條數(shù)據(jù)微渠,當前顯示第n頁, 求總頁數(shù):
查詢總條數(shù)p1
使用p1除以m得到p2
如果整除則p2為總數(shù)頁
如果不整除則p2+1為總頁數(shù)
求第n頁的數(shù)據(jù)
select * from 表名 limit start,count
select * from students where isdelete=0 limit (n-1)*m,m