查詢語句結構及執行順序
3、
select
…聚合函數from
表名
1、where
…
2、group by
…
4、having
…
5、order by
…
6、limit
…;
- 聚合函數
方法 | 功能 |
---|---|
avg(字段名) | 該字段的平均值 |
max(字段名) | 該字段的最大值 |
min(字段名) | 該字段的最小值 |
sum(字段名) | 該字段所有記錄的和 |
count(字段名) | 統計該字段記錄的個數 |
count(*) 可統計所有數據,包括值爲null的數據 |
例如1 : 找出表中的最大攻擊力的值?
select max(attack) from sanguo;
例如2 : 表中共有多少個英雄?
select count(*) as number from sanguo;
例如3 : 蜀國英雄中攻擊值大於200的英雄的數量
select count(id) from sanguo where country='蜀國' and attack > 200;
- group by
給查詢的結果進行分組
例如1 : 計算每個國家的平均攻擊力
select country , avg(attack) from sanguo group by country;
例如2 : 所有國家的男英雄中 英雄數量最多的前2名的 國家名稱及英雄數量
select country, count(id) as number from sanguo
where gender = 'M'
group by country
order by number DESC
limit 2
group by後字段名必須要爲select後的字段
查詢字段和group by後字段不一致,則必須對該字段進行聚合處理(聚合函數)
- having語句
對分組聚合後的結果進行進一步篩選
例如 : 找出平均攻擊力大於105的國家的前2名,顯示國家名稱和平均攻擊力
select country, avg(attack) from sanguo
group by country
having avg(attack) > 105
order by avg(attack) DESC
limit 2;
注意:
having語句通常與group by聯合使用,having語句存在彌補了where關鍵字不能與聚合函數聯合使用的不足where只能操作表中實際存在的字段,having操作的是聚合函數生成的顯示列
- distinct語句
不顯示字段重複值
例如1: 表中都有哪些國家
select distinct country from sanguo;
例如2 : 計算一共有多少個國家
select count(distinct country) from sanguo;
注意
distinct和from之間所有字段都相同纔會去重
distinct不能對任何字段做聚合處理
- 查詢表記錄時做數學運算
運算符 : + - * / % **
例如1: 查詢時顯示攻擊力翻倍
select attack*2 from sanguo
例如2: 更新蜀國所有英雄攻擊力 * 2
update sanguo set attack = attack*2 where country='蜀國'
索引
-
定義
對數據庫表的一列或多列的值進行排序的一種結構(Btree方式) -
優點
加快數據檢索速度
-
缺點
佔用物理存儲空間(/var/lib/mysql)
當對錶中數據更新時,索引需要動態維護,降低數據維護速度
- 索引示例
1、開啓運行時間檢測
mysql>show variables like '%pro%';
mysql>set profiling=1;
2、執行查詢語句(無索引)
select name from students where name='Tom99999';
3、查看執行時間
show profiles;
4、在name字段創建索引
create index name on students(name);
5、再執行查詢語句
select name from students where name='Tom88888';
6、查看執行時間
show profiles;
索引分類
普通(MUL) and 唯一(UNI)
- 使用規則
1、可設置多個字段
2、普通索引 :字段值無約束,KEY標誌爲MUL
3、唯一索引(unique
) :字段值不允許重複,但可爲NULL
,KEY標誌爲UNI
4、哪些字段創建索引:經常用來查詢的字段、where
條件判斷字段、order by
排序字段
- 創建普通索引and唯一索引
創建表時
create table 表名(
字段名 數據類型,
字段名 數據類型,
index(字段名),
index(字段名),
unique(字段名)
);
已有表中創建
create [unique] index 索引名 on 表名(字段名);
- 查看索引
1、
desc 表名;
KEY標誌爲:MUL
、UNI
2、show index from 表名\G;
- 刪除索引
drop index 索引名 on 表名;
聯合索引
- 概念
聯合索引又叫複合索引,即一個覆蓋表中兩列或者以上的索引。
例如:index_name(column a,column b)
- 創建方式
方式一:
alter table table_name add index index_name(column_list)
方式二:create index index_name on table_name(column_list)
index_name
是創建的聯合索引的名字,可以沒有,沒有的話系統會根據該索引包含的第一列來賦值名稱;
table_name
是要創建該索引的表名;
column_list
是該索引所包含的表的字段名。
例如:
create table stu
(
id int,
name varchar(10),
age int,
primary key(id)
);
ALTER TABLE stu ADD INDEX LianHeIndex (name,age);
或者
create index LianHeIndex on stu(name,age);
注意:
一、聯合索引遵守一個最左原則
index LianHeIndex (name,age),該索引支持name或者name age組合查詢,而不支持age單獨查詢
二、最佳左前綴特性
如果我們是在name
和age
上分別創建單個索引的話,由於mysql查詢每次只能使用一個索引
,所以雖然這樣已經相對不做索引時全表掃描提高了很多效率,但是如果在
name、age兩列上創建複合索引的話將帶來更高的效率。如果我們創建了(name, age)的複合索引,那麼其實相當於創建了(name)、(name,age)兩個索引,因此我們在創建複合索引時應該將最常用作限制條件的列放在最左邊,依次遞減。
三、只要列中包含有NULL
值,該列對於聯合索引時無效的
主鍵(PRI)and自增長(auto_increment)
- 使用規則
1、只能有一個主鍵字段
2、所帶約束 :不允許重複,且不能爲NULL
3、KEY標誌(primary
) :PRI
4、通常設置記錄編號字段id
,能唯一鎖定一條記錄
- 創建
創建表添加主鍵
create table student(
id int auto_increment,
name varchar(20),
primary key(id)
)charset=utf8,auto_increment=10000;##設置自增長起始值
已有表添加主鍵
alter table 表名 add primary key(id);
已有表操作自增長屬性
1、已有表添加自增長屬性
alter table 表名 modify id int auto_increment;
2、已有表重新指定起始值:
alter table 表名 auto_increment=20000;
- 刪除
1、刪除自增長屬性(
modify
)
alter table 表名 modify id int;
2、刪除主鍵索引
alter table 表名 drop primary key;