Num01-->mysql基本查詢
1、查詢所有字段
------
select * from 表名;
例:
select * from students;
2、查詢指定字段
------
在select后面的列名部分,可以使用as為列起別名,這個別名出現在結果集中
select 列1,列2,... from 表名;
例:
select id,name,gender from students;
3、消除重復行
-------
在select后面列前使用distinct可以消除重復的行
select distinct 列1,... from 表名;
例:
select distinct gender from students;
Num02-->mysql條件查詢
1、使用where子句對表中的數據篩選,結果為true的行會出現在結果集中
-------------------------------------
語法如下:
select * from 表名 where 條件;
例:
select * from students where id=1;
2、where后面支持多種運算符,進行條件的處理,如下
---------------------------
比較運算符
邏輯運算符
模糊查詢
范圍查詢
空判斷
2.1比較運算符
--------
等于=
大于>
大于等于>=
小于<
小于等于<=
不等于!=或<>
例1:查詢編號大于3的學生
select * from students where id>3;
例2:查詢編號不大于4的科目
select * from subjects where id<=4;
例3:查詢姓名不是“黃蓉”的學生
select * from students where name!='黃蓉';
例4:查詢沒被刪除的學生
select * from students where isdelete=0;
2.2邏輯運算符
--------
and
or
not
例5:查詢編號大于3的女同學
select * from students where id>3 and gender=0;
例6:查詢編號小于4或沒被刪除的學生
select * from students where id<4 or isdelete=0;
2.3模糊查詢
-------
like
%表示任意多個任意字符
_表示一個任意字符
例7:查詢姓黃的學生
select * from students where name like '黃%';
例8:查詢姓黃并且名字是一個字的學生
select * from students where name like '黃_';
例9:查詢姓黃或叫靖的學生
select * from students where name like '黃%' or name like '%靖';
2.4范圍查詢
-------
in表示在一個非連續的范圍內
例10:查詢編號是1或3或8的學生
select * from students where id in(1,3,8);
between ... and ...表示在一個連續的范圍內
例11:查詢編號為3至8的學生
select * from students where id between 3 and 8;
例12:查詢學生是3至8的男生
select * from students where id between 3 and 8 and gender=1;
2.5空判斷
------
注意:null與'' 是不同的
判空is null
例13:查詢沒有填寫地址的學生
select * from students where hometown is null;
判非空is not null
例14:查詢填寫了地址的學生
select * from students where hometown is not null;
例15:查詢填寫了地址的女生
select * from students where hometown is not null and gender=0;
3、優先級
-----
優先級由高到低的順序為:小括號,not,比較運算符,邏輯運算符
and比or先運算,如果同時出現并希望先算or,需要結合()使用
Num03-->mysql聚合函數
為了快速得到統計數據,經常會用到如下5個聚合函數
1、count(*)表示計算總行數,括號中寫星與列名,結果是相同的
例1:查詢學生總數
select count(*) from students;
2、max(列)表示求此列的最大值
例2:查詢女生的編號最大值
select max(id) from students where gender=0;
3、min(列)表示求此列的最小值
例3:查詢未刪除的學生最小編號
select min(id) from students where isdelete=0;
4、sum(列)表示求此列的和
例4:查詢男生的編號之和
select sum(id) from students where gender=1;
5、avg(列)表示求此列的平均值
例5:查詢未刪除女生的編號平均值
select avg(id) from students where isdelete=0 and gender=0;
Num04-->mysql分組
按照字段分組,表示此字段相同的數據會被放到一個組中
分組后,分組的依據列會顯示在結果集中,其他列不會顯示在結果集中
可以對分組后的數據進行統計,做聚合運算
語法:
select 列1,列2,聚合... from 表名 group by 列1,列2...
例1:查詢男女生總數
select gender as 性別,count(*) from students group by gender;
例2:查詢各城市人數
select hometown as 家鄉,count(*) from students group by hometown;
分組后的數據篩選
語法:
select 列1,列2,聚合... from 表名 group by 列1,列2,列3... having 列1,...聚合...
having后面的條件運算符與where的相同
例3:查詢男生總人數
方案一
select count(*) from students where gender=1;
-----------------------------------
方案二:
select gender as 性別,count(*) from students group by gender
having gender=1;
where與having的區別
---------------
where是對from后面指定的表進行數據篩選,屬于對原始數據的篩選
having是對group by的結果進行篩選
Num05-->mysql排序
為了方便查看數據,可以對數據進行排序
語法:
select * from 表名
order by 列1 asc|desc,列2 asc|desc,...;
將行數據按照列1進行排序,如果某些行列1的值相同時,則按照列2排序,以此類推
默認按照列值從小到大排列
asc從小到大排列,即升序
desc從大到小排序,即降序
例1:查詢未刪除男生信息,按學號降序
select * from students where gender=1 and isdelete=0 order by id desc;
例2:查詢未刪除科目信息,按名稱升序
select * from subjects where isdelete=0 order by name;
Num06-->mysql分頁
當數據量過大時,在一頁中查看數據是一件非常麻煩的事情
語法:
select * from 表名 limit start,count
從start開始,獲取count條數據
start索引從0開始
例1:查詢前3行男生信息
select * from students where gender=1 limit 0,3;
示例:
已知:每頁顯示m條數據,當前顯示第n頁
求總頁數:此段邏輯后面會在python中實現
查詢總條數p1
使用p1除以m得到p2
如果整除則p2為總數頁
如果不整除則p2+1為總頁數
求第n頁的數據
select * from students where isdelete=0 limit (n-1)*m,m;
Num07-->mysql連接查詢
當查詢結果的列來源于多張表時,需要將多張表連接成一個大的數據集,再選擇合適的列返回
mysql支持三種類型的連接查詢,分別為:
1、內連接查詢:查詢的結果為兩個表匹配到的數據
2、左連接查詢:查詢的結果為兩個表匹配到的數據,左表特有的數據,對于右表中不存在的數據使用null填充
3、右連接查詢:查詢的結果為兩個表匹配到的數據,右表特有的數據,對于左表中不存在的數據使用null填充
語法:
select * from 表1
inner或left或right join 表2 on 表1.列=表2.列
例1:使用內連接查詢班級表與學生表
此處使用了as為表起別名,目的是編寫簡單
select * from classes as cls
inner join students as stu on sub.id=stu.cls.id
例2:使用左連接查詢班級表與學生表
select * from classes as cls
left join students as stu on sub.id=stu.cls.id
例3:使用右連接查詢班級表與學生表
select * from classes as cls
right join students as stu on sub.id=stu.cls.id
例4:查詢學生姓名及班級名稱
select stu.name as stuname,cls.name as clsname from classes as cls
inner join students as stu on sub.id=stu.cls.id
Num08-->mysql自關聯
設計省信息的表結構provinces
id
ptitle
設計市信息的表結構citys
id
ctitle
proid
citys表的proid表示城市所屬的省,對應著provinces表的id值
問題:能不能將兩個表合成一張表呢?
思考:觀察兩張表發現,citys表比provinces表多一個列proid,其它列的類型都是一樣的
意義:存儲的都是地區信息,而且每種信息的數據量有限,沒必要增加一個新表,或者將來還要存儲區、鄉鎮信息,都增加新表的開銷太大
答案:定義表areas,結構如下
id
atitle
pid
因為省沒有所屬的省份,所以可以填寫為null
城市所屬的省份pid,填寫省所對應的編號id
這就是自關聯,表中的某一列,關聯了這個表中的另外一列,但是它們的業務邏輯含義是不一樣的,城市信息的pid引用的是省信息的id
在這個表中,結構不變,可以添加區縣、鄉鎮街道、村社區等信息
創建areas表的語句如下:
create table areas(
aid int primary key,
atitle varchar(20),
pid int
);
從sql文件中導入數據
source areas.sql;
查詢一共有多少個省
select count(*) from areas where pid is null;
例1:查詢省的名稱為“山西省”的所有城市
select city.* from areas as city
inner join areas as province on city.pid=province.aid
where province.atitle='山西省';
例2:查詢市的名稱為“廣州市”的所有區縣
select dis.* from areas as dis
inner join areas as city on city.aid=dis.pid
where city.atitle='廣州市';
Num09-->mysql子查詢
在一個select語句中,可以嵌套另一個select語句,這種查詢語句被稱為子查詢
子查詢主要用到三個位置
作為select子名
作為from子句
作為where子句
例1:查詢學生姓名及班級名稱
select stu.name as stuname,
(select cls.name from classes where cls.id=stu.clsid) as clsname
from students as stu;
例2:查詢學生與班級對應的信息
select * from
(select stu.*,cls.name as clsname from students as stu
inner join classes as cls on stu.clsid=cls.id)
as t1;
例3:查詢班級名稱為'python1'的所有學生姓名
select stu.name from students as stu
where stu.clsid=(select id from classes where name='python1')
說明:發現很多子查詢的語句,都是可以使用連接查詢實現的,此時推薦使用連接查詢,因為連接查詢的語句更簡潔,邏輯更清晰
Num10-->mysql完整select語句
select distinct*
from 表名
inner 或left或right join ...on...
where...
group by... having...
order by...
limit start,count;