SELECT語句的執(zhí)行過程(單表查詢)
過程如下 :
1、先執(zhí)行FROM,先找表,先確定對(duì)應(yīng)數(shù)據(jù)庫中的表
SELECT
SELECT [ALL | DISTINCT | DISTINCTROW ] [SQL_CACHE | SQL_NO_CACHE] select_expr [, select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [FOR UPDATE | LOCK IN SHARE MODE]
總結(jié):
1、字段顯示可以顯示別名
count]:對(duì)查詢的結(jié)果進(jìn)行輸出行數(shù)數(shù)量限制
:單個(gè)任意字符
示例:
查找指定表中的所有字段
seletc * from students
查找students表中的所有數(shù)據(jù),顯示指定的行
select * from students limit 6;
查找指定字段行
select name,age from students;
查找tb1表中age 不等于24 的數(shù)據(jù)
select * from stdents where age != 24;
查找指定表中age大于等于24并且小于等于20的數(shù)據(jù)
selsct * from students where age>=24 and age <=20; select * fromstudents where age between 20 and 24;
查找指定表中age等于20或者等于24的數(shù)據(jù)
select * from students where age=20 or age=24;
查找指定表中的查詢出age 不在20到24的區(qū)間中的數(shù)據(jù)
select * from students where age <25 or age >24; select * from students where not between 20 and 24;
通過like 結(jié)合通配符進(jìn)行模糊搜索
查找所有字段開頭是s的任意長度數(shù)據(jù)
select * frim students where name like 's%';
查找所有字段開始是s,但是只有三個(gè)字符的數(shù)據(jù)
select * from students where name like 's__'
通過rlike結(jié)合正則表達(dá)式
查找所有字段開頭是s的任意長度數(shù)據(jù)
select * from students where name rlike ‘^s.*’
查找指定字段匹配對(duì)應(yīng)的條件,使用in關(guān)鍵字指定
查找指定表中age 等于21 20 23 的數(shù)據(jù)
select * from students where age in (20,21,23);
查找指定表中age不等于21 20 23 的數(shù)據(jù)
select * from students where age not in (20,21,23);
對(duì)指定字段進(jìn)行排序
查找所有表數(shù)據(jù),并且指定字段進(jìn)行排序
查詢studentsd 的所有數(shù)據(jù),并指定age字段的值從大到小進(jìn)行降序排序,如果多行之間的age字段相同時(shí),在將name字段進(jìn)行升序排序
select * from students order by age desc, name asc;
注意:
order by 后面跟的要排序的字段,順序不一樣,結(jié)果也不一樣
比如:
1、select * from students order by age,classid; 2、select * from students order by classid,age; 第一條是先對(duì)age進(jìn)行排序后,再對(duì)classid進(jìn)行排序 第二條是先對(duì)classid進(jìn)行排序,然后再對(duì)age進(jìn)行排序
去重
使用DISTINCT關(guān)鍵字進(jìn)行去重
select distinct age from students;
別名
查詢時(shí)給字段添加別名,顯示的時(shí)候顯示別名
select name as Name,age from students;
分組
分組
GROUP:根據(jù)指定的條件把查詢結(jié)果進(jìn)行“分組”以用于做“聚合”運(yùn)算
avg(), 統(tǒng)計(jì)最小值 max(), 統(tǒng)計(jì)最大值 min(), 統(tǒng)計(jì)最小值 count(), 統(tǒng)計(jì)每個(gè)分組的數(shù)量 sum() 統(tǒng)計(jì)每個(gè)分組的總和 HAVING: 對(duì)分組聚合運(yùn)算后的結(jié)果指定過濾條件
示例:
對(duì)sudents表的gender字段進(jìn)行分組
select * from students group by gender;
需要注意的是,顯示的是第一次查找到男生和女生數(shù)據(jù)
聚合操作
select count(*),gender from students group by gender;
先對(duì)性別進(jìn)行分組,然后統(tǒng)計(jì)每組中的人數(shù)。
count()就是一種聚合函數(shù),這個(gè)函數(shù)能統(tǒng)計(jì)數(shù)量
先對(duì)性別進(jìn)行分組,然后將每個(gè)分組中的age字段進(jìn)行平均計(jì)算,得到每個(gè)組的平均年齡
select avg(age),gender from students group by gender;
group_concat()用法
select gender,group_concat(name) from students group by gender;
先將性別分組后,然后顯示男生和女生組中的名字
HAVING用法
select gender,avg(age) from students group by gender having avg(age) > 20;
將分組過后的信息在進(jìn)行條件過濾,