每日分享:
總有一天,你會明白,能夠治愈你的,從來都不是時間,而是你心里的那股釋懷和格局,只要內心不慌亂,連世界都影響不了你,
目錄
一、排序
二、分頁查詢
三、聚合函式
四、分組查詢
五、連接查詢
1. 內連接查詢
2. 左連接查詢
3. 右連接查詢
4. 自連接查詢
六、子查詢
一、排序
排序查詢語法:
select * from 表名 order by 列1 asc/desc [,列2 asc/desc,...]
語法說明:
- 先按照列1進行排序,如果列1的值相同時,則按照列2排序
- asc:升序排序(從小到大)
- desc:降序排序(從大到小)
- 默認是升序排序(asc)
查詢未洗掉男生資訊,按學號降序:
select * from students where is_del = 0 and sex = '男' order by id desc;

顯示所有學生資訊,先按年齡從大到小排序,年齡相同時按身高由高到低排序:
select * from students order by age desc,height desc;

二、分頁查詢
在網上購物時,瀏覽商品串列的時候,由于資料特別多,一頁顯示不完,一頁一頁的進行顯示,這就是分頁查詢
select * from 表名 limit start,count
說明:
- limit 是分頁查詢關鍵字
- start 表示開始行索引,默認是0
- count 表示查詢條數
查詢前三行男生的資訊:
select * from students where sex='男' limit 0,3;
可以簡寫為
select * from students where sex='男' limit 3;


每頁顯示m條資料,求第n頁顯示的資料(關鍵是求每頁的開始行索引)
select * from students limit (n-1)*m,m;
三、聚合函式
聚合函式又叫組函式,通常是對表中的資料進行統計和計算,一般結合分組(group by)來使用,用于統計和計算分組資料
常用的聚合函式:
- count(col):表示求指定列的總行數
- max(col):表示求指定列的最大值
- min(col):表示求指定列的最小值
- sum(col):表示求指定列的和
- avg(col):表示指定列的平均值
求總行數:
回傳非null資料的總行數
select count(height) from students;
回傳總行數,包含null值記錄
select count(*) from students;

求最大值:
查詢男生編號的最大值
select max(id) from students where sex='男';

求最小值:
查詢未洗掉的學生最小編號
select min(id) from students where is_del=0;

求和:
查詢男生的總身高
select sum(height) from students where sex='男';
查詢男生的平均身高
select sum(height) / count(*) from students where sex='男';

求平均值:
求男生的平均身高,聚合函式不統計null值
select avg(height) from students where sex='男';
求男生的平均身高,包含身高為null的值
select avg(ifnull(height,0)) from students where sex='男';
說明:
- ifnull函式:表示判斷指定欄位的值是否為null,如果為空則使用自己提供的值

聚合函式特點:
- 聚合函式默認忽略欄位為null的記錄,要想列值為null的記錄也參與計算,必須使用ifnull函式對null值做替換
四、分組查詢
分組查詢就是將查詢結果按照指定欄位進行分組,欄位中資料相等的分為一組
分組查詢基本的語法格式:
group by 列名 [having 條件運算式] [with rollup]
說明:
- 列名:是指按照指定欄位的值進行分組
- having 條件運算式:用來過濾分組后的資料
- with rollup:在所有記錄的最后加上一條記錄,顯示select查詢時聚合函式的統計和計算結果
group by 的使用:
group by可用于單個欄位分組,也可用于多個欄位分組
根據sex欄位來分組
select gender from students group by sex;
根據name和sex欄位來分組
select name,sex from students group by name,sex;

group by + group_concat()的使用:
group_concat(欄位名):統計每個分組指定欄位的資訊集合,每個資訊之間用逗號分割
根據sex欄位進行分組,查詢sex欄位和分組的name欄位資訊
select sex,group_concat(name) from students group by sex;

group by + 聚合函式的使用:
統計不同性別的人的平均年齡
select sex,avg(age) from students group by sex;
統計不同性別的人的個數
select sex,count(*) from students group by sex;

group by + having的使用:
having作用和where類似都是過濾資料的,但having是過濾分組資料的,只能用于group by
根據sex欄位進行分組,統計分組條數大于2的
select sex,count(*) from students group by sex having count(*)>2;

group by + with rollup的使用:
with rollup的作用是:在最后記錄后面新增一行,顯示select查詢時聚合函式的統計和計算結果
根據sex欄位進行分組,匯總總人數
select sex,count(*) from students group by sex with rollup;
根據sex欄位進行分組,匯總所有人年齡
select sex,group_concat(age) from students group by sex with rollup;


小結:
- group by 根據指定的一個或者多個欄位對資料進行分組
- group_concat(欄位名)函式是統計每個分組指定欄位的資訊集合
- 聚合函式在和group by 結合使用時,聚合函式統計和計算的是每個分組的資料
- having 是對分組資料進行條件過濾
- with rollup 在最后記錄后面新增一行,顯示select查詢時聚合函式的統計和計算結果
五、連接查詢
連接查詢可以實作多個表的查詢,當查詢的欄位資料來自不同的表就可以使用連接查詢來完成
連接查詢分為:
- 內連接查詢
- 左連接查詢
- 右連接查詢
- 自連接查詢
1. 內連接查詢
查詢兩個表中符合條件的共有記錄(取交集)
內連接查詢語法格式:
select 欄位 from 表1 inner join 表2 on 表1.欄位1 = 表2.欄位2
說明:
- inner join 就是內連接查詢關鍵字
- on 就是連接查詢條件
使用內連接查詢學生表與班級表:
select * from students s inner join classes c on s.c_id = c.id;
原本兩個表的內容:


使用內連接:


2. 左連接查詢
以左表為主根據條件查詢右表資料,如果根據條件查詢右表資料不存在則使用null值填充
左連接查詢語法格式:
select 欄位 from 表1 left join 表2 on 表1.欄位1 = 表2.欄位2
說明:
- left join 就是左連接查詢關鍵字
- on 就是連接查詢條件
- 表1 是左表
- 表2 是右表
使用左連接查詢學生表與班級表:
select * from students s left join classes c on s.c_id = c.id;

3. 右連接查詢
以右表為主根據條件查詢左表資料,如果根據條件查詢左表資料不存在則使用null值填充
右連接查詢語法格式:
select 欄位 from 表1 right join 表2 on 表1.欄位1 = 表2.欄位2;
說明:
- right join 就是右連接查詢關鍵字
- on 就是連接查詢條件
- 表1 是左表
- 表2 是右表
使用右連接查詢學生表與班級表:
select * from students s right join classes c on s.c_id = c.id;


4. 自連接查詢
左表和右表是同一個表,根據連接查詢條件查詢兩個表中的資料

創建areas表:
create table areas(
id varchar(20) not null primary key,
title varchar(30) not null,
pid varchar(20)
);
執行sql檔案給areas表匯入資料:
source areas.sql;
sql檔案內容:
insert into areas values('11000', '北京市', null);
insert into areas values('11001', '北京市', '11000');
insert into areas values('11002', '東城區', '11001');
insert into areas values('11003', '西城區', '11001');
insert into areas values('11004', '朝陽區', '11001');
insert into areas values('11005', '豐臺區', '11001');
insert into areas values('11006', '海淀區', '11001');
insert into areas values('12000', '河北省', null);
insert into areas values('12001', '石家莊市', '12000');
insert into areas values('12002', '長安區', '12001');
insert into areas values('12003', '橋東區', '12001');
insert into areas values('12004', '橋西區', '12001');
insert into areas values('12005', '新華區', '12001');
說明:
- source 表示執行的sql檔案
自連接查詢的用法:
select c.id, c.title, c.pid, p.title from areas c inner join areas p on c.pid = p.id;
說明:
- 自連接查詢必須對表起別名

六、子查詢
在一個select陳述句中,嵌入了另外一個select陳述句,那么被嵌入的select陳述句稱之為子查詢陳述句,外部的那個select陳述句則稱為主查詢
主查詢和子查詢的關系:
- 子查詢是嵌入到主查詢中
- 子查詢是輔助主查詢的,要么充當條件,要么充當資料源
- 子查詢是可以獨立存在的陳述句,是一條完整的select陳述句
查詢大于平均年齡的學生:
select * from students where age > (select avg(age) from students);

查詢學生在班的所有班級名字:
select name from classes where id in (select c_id from students where c_id is not null);

查找年齡最大,身高最高的學生:
select * from students where age=(select max(age) from students) and height=(select max(height) from students);
可以簡寫為:
select * from students where (age,height) = (select max(age), max(height) from students);

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/423403.html
標籤:其他
上一篇:MaxCompute SQL函式詳解 ODPS SQL函式詳解---之常用數學運算相關函式
下一篇:hive 學習筆記
