資料查詢
資料查詢是資料庫系統應用的主要內容,也是用戶對資料庫最頻繁、最常見的基本操作請求,資料查詢可以根據用戶提供的限定條件,從已存在的資料表中檢索用戶需要的資料,MySQL使用SELECT陳述句從資料庫中檢索資料,并將結果集以表格的形式回傳給用戶,
SELECT查詢的基本語法
select * from 表名;
from關鍵字后面寫表名,表示資料來源于是這張表
select后面寫表中的列名,如果是*表示在結果中顯示表中所有列
在select后面的列名部分,可以使用as為列起別名,這個別名出現在結果集中
如果要查詢多個列,之間使用逗號分隔
消除重復行
- 在select后面列前使用distinct可以消除重復的行
select distinct gender from students;
條件
- 使用where子句對表中的資料篩選,結果為true的行會出現在結果集中
- 語法如下:
select * from 表名 where 條件;
比較運算子
- 等于=
- 大于>
- 大于等于>=
- 小于<
- 小于等于<=
- 不等于!=或<>
- 查詢編號大于3的學生
select * from students where id>3;
- 查詢編號不大于4的科目
select * from subjects where id<=4;
- 查詢姓名不是“黃蓉”的學生
select * from students where sname!='黃蓉';
- 查詢沒被洗掉的學生
select * from students where isdelete=0;
邏輯運算子
- and
- or
- not
- 查詢編號大于3的**學
select * from students where id>3 and gender=0;
- 查詢編號小于4或沒被洗掉的學生
select * from students where id<4 or isdelete=0;
模糊查詢
- like%表示任意多個任意字符
- _表示一個任意字符
查詢卸訓的學生
注意:可能出現兩個_代表一個漢字的情況;
select * from students where sname like '黃%';
- 查詢卸訓并且名字是一個字的學生
select * from students where sname like '黃_';
- 查詢卸訓或叫靖的學生
select * from students where sname like '黃%' or sname like '%靖%';
范圍查詢
in表示在一個非連續的范圍內
查詢編號是1或3或8的學生
select * from students where id in(1,3,8); //括號內的值可以實際不存在,但是沒意義
- between ... and ...表示在一個連續的范圍內
- 查詢學生是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;
空判斷
- 注意:null與''是不同的
- 判空is null
- 查詢沒有填寫地址的學生
select * from students where hometown is null;
- 判非空is not null
- 查詢填寫了地址的學生
select * from students where hometown is not null;
- 查詢填寫了地址的女生
select * from students where hometown is not null and gender=0;
優先級
- 小括號,not,比較運算子,邏輯運算子
- and比or先運算,如果同時出現并希望先算or,需要結合()使用
聚合
能看到統計的結果看不到原始資料
為了快速得到統計資料,提供了5個聚合函式
count(*)表示計算總行數,括號中寫星與列名,結果是相同的
查詢學生總數
select count(*) from students;
- max(列)表示求此列的最大值
- 查詢女生的編號最大值
select max(id) from students where gender=0;
- min(列)表示求此列的最小值
- 查詢未洗掉的學生最小編號
select min(id) from students where isdelete=0;
- sum(列)表示求此列的和 //數值型別的列求和
- 查詢男生的編號之后
select sum(id) from students where gender=1;
- avg(列)表示求此列的平均值 //數值型別的列求平均值
- 查詢未洗掉女生的編號平均值
select avg(id) from students where isdelete=0 and gender=0;
分組
group by分組的目的還是聚合
按照欄位分組,表示此欄位相同的資料會被放到一個組中 //篩選
分組后,只能查詢出相同的資料列,對于有差異的資料列無法出現在一個結果集中
可以對分組后的資料進行統計,做聚合運算
語法:
select 列1,列2,聚合... from 表名 group by 列1,列2,列3... //將列123都一樣放到一組
- 查詢男女生總數
select gender as 性別,count(*)from studentsgroup by gender;
- 查詢各城市人數
select hometown as 家鄉,count(*)from studentsgroup by hometown;
分組后的資料篩選
- 語法:
select 列1,列2,聚合... from 表名group by 列1,列2,列3...having 列1,...聚合...
- having后面的條件運算子與where的相同
- 查詢男生總人數
方案一select count(*)from studentswhere gender=1;
方案二//優點 可以更為直觀的查看篩選結果
select gender as 性別,count(*)from studentsgroup by genderhaving gender=1;
對比where與having
where是對from后面指定的表進行資料篩選,屬于對原始資料的篩選
having是對group by的結果進行篩選
排序
- 為了方便查看資料,可以對資料進行排序
- 語法:
select * from 表名order by 列1 asc|desc,列2 asc|desc,...
將行資料按照列1進行排序,如果某些行列1的值相同時,則按照列2排序,以此類推
默認按照列值從小到大排列
asc從小到大排列,即升序 //ascend
desc從大到小排序,即降序 //descend
查詢未洗掉男生學生資訊,按學號降序
select * from studentswhere gender=1 and isdelete=0order by id desc;
- 查詢未洗掉科目資訊,按名稱升序
| 1 | select * from subjectwhere isdelete=0order by stitle; |
獲取部分行
當資料量過大時,在一頁中查看資料是一件非常麻煩的事情
語法
select * from 表名limit start,count
- 從start開始,獲取count條資料
- start索引從0開始 //從哪兒開始數幾個
示例:分頁
已知:每頁顯示m條資料,當前顯示第n頁
求總頁數:此段邏輯后面會在python中實作
-
- 查詢總條數p1
- 使用p1除以m得到p2
- 如果整除則p2為總數頁
- 如果不整除則p2+1為總頁數
- 求第n頁的資料
| 1 | select * from studentswhere isdelete=0limit (n-1)*m,m |
總結
- 完整的select陳述句
select distinct *from 表名where ....group by ... having ...order by ...limit star,count
- 執行順序為:
- from 表名
- where ....
- group by ...
- select distinct *
- having ...
- order by ...
- limit star,count
- 實際使用中,只是陳述句中某些部分的組合,而不是全部
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/163637.html
標籤:其他
上一篇:垃圾回識訓制
