業(yè)務(wù)中最常見(jiàn)的執(zhí)行操作就是查詢
查詢語(yǔ)句執(zhí)行完的結(jié)果依然是一張新的表住拭,結(jié)果集
一枣察、基本查詢語(yǔ)句
- 一般查詢
語(yǔ)法:表的 哪些列
select 字段1帆离,字段2,字段3.. from 表名
示例:
select `stu_name`,`stu_age` from stu_info;
* 號(hào)表示所有字段
select * from stu_info ; //查詢所有列
-
條件查詢
語(yǔ)法:表眨攘,哪些行,哪些列
select 字段1许师,字段2屏轰,..from 表名 where 條件
示例:
- 查詢所有的男生姓名
select `stu_name` from stu_info where `stu_sex`=1
- 查詢所有男生的姓名和年齡
select `stu_name`,`stu_age` from stu_info where `stu_sex`=1
條件
where 條件表達(dá)式 變量 => 字段名 常量 => 數(shù)值、字符串 運(yùn)算符 => 參照以下表格
運(yùn)算符
運(yùn)算符 含義 用法表達(dá)式 查詢結(jié)果 = 全等比較 stu_sex
= 1查詢所有的男生信息 <>, != 不等于 stu_sex
<>1查詢所有的非男生信息 >,>=,<,<= 大小關(guān)系比較 stu_age
>=20查詢所有的年齡在20及20歲以上的學(xué)生信息 is null 判斷為空 c_id
is null查詢所有c_id項(xiàng)為NULL的數(shù)據(jù)行叔遂,(未分班學(xué)生) is not null 判斷非空 c_id
is not null查詢所有c_id項(xiàng)非NULL的數(shù)據(jù)行他炊,(已分班學(xué)生) between ... and ... 值在某個(gè)區(qū)間 stu_age
between 18 and 25查詢所有年齡在18到25歲之間的學(xué)生信息 not 邏輯非 not c_id
='c001'查詢除了3班以外學(xué)生信息 or 邏輯或 c_id
='c001' orc_id
='c002'查詢c001和c002班學(xué)生的信息 and 邏輯與 c_id='c003' and stu_sex
=0查詢c003班所有女生信息 like 模糊等于 stu_name
like '張%'"字符%" 以指定字符開(kāi)頭
"%字符" 以指定字符結(jié)尾
"%字符%" 包含指定字符() 提升表達(dá)式優(yōu)先級(jí) stu_age
> (18 + 2)查詢年齡大于20的學(xué)生信息 練習(xí)
-
查詢c003班和c002班,年齡在18-25周歲之間的張姓女同學(xué) 年齡已艰,姓名 痊末,班級(jí)信息
select * from stu_info where (`c_id`='c_003' or `c_id`='c_002') and (`stu_age` between 18 and 25) and (`stu_name` like '張%') and (`stu_sex`=0)
- 查詢所有已分班的學(xué)生中,年齡小于18歲的男同學(xué)哩掺,基本信息
二凿叠、進(jìn)階查詢
-
排序查詢
語(yǔ)法: 查詢結(jié)果集 +排序語(yǔ)句
select 語(yǔ)句 order by 排序依據(jù)字段 asc / desc
- 升序/降序
查詢c_003班學(xué)生信息,并按照年齡進(jìn)行升序/降序
select * from stu_info where `c_id`='c_003' order by `stu_age` asc //升 select * from stu_info where `c_id`='c_003' order by `stu_age` desc //降
排序語(yǔ)句永遠(yuǎn)寫(xiě)在查詢語(yǔ)句之后,是查詢語(yǔ)句的結(jié)果集進(jìn)行操作盒件,生成新的結(jié)果集
-
限定查詢 limit 起始數(shù)據(jù)下標(biāo), 截取數(shù)據(jù)長(zhǎng)度
對(duì)查詢結(jié)果集進(jìn)行分段截取
select 基本查詢語(yǔ)句 limit 結(jié)果集行標(biāo)蹬碧,數(shù)量
示例:
查詢c_002班學(xué)生 年齡升序,取前兩個(gè)數(shù)據(jù)
select * from stu_info where 'c_id'='c_002' order by `stu_age` asc limit 0,2
聚合函數(shù)(統(tǒng)計(jì)查詢)
語(yǔ)法:對(duì)字段數(shù)據(jù)進(jìn)行統(tǒng)計(jì)
select 聚合函數(shù)(字段名) as 新字段名 from 表 where 條件
- 平均值 avg() 使用 as關(guān)鍵字對(duì)查詢結(jié)果集字段進(jìn)行重命名
select avg(`stu_age`) as `avgage` from stu_info where `c_id`='c_003'
- 數(shù)據(jù)列求和sum()
select sum(`stu_age`) as `totalAge` from stu_info where `c_id`='c_001'
- 最大值 max()
select max(`stu_age`) as `maxAge` from stu_info ;
- 最小值min()
select min(`stu_age`) as `minAge` from stu_info ;
- 統(tǒng)計(jì)字段的數(shù)據(jù)總數(shù) count(id)
獲取c_003班學(xué)生人數(shù)
select count(`stu_id`) as `3班總?cè)藬?shù)` from stu_info where `c_id`='c_003'
-
分組查詢 group by
select 聚合函數(shù)(統(tǒng)計(jì)的字段),分組的字段 from 表 where 條件 group by 分組的字段
示例:
查詢c_003班炒刁,男生女生的人數(shù)
分析恩沽,統(tǒng)計(jì)數(shù)量 count(統(tǒng)計(jì)字段:stu_id) ,分組 按stu_sex字段分組
select count(`stu_id`) as `stuCount`,`stu_sex` from stu_info where `c_id`='c_003' group by `stu_sex`
查詢每個(gè)班的最大年齡和最小年齡
select max(`stu_age`) as `最大年齡`, min(`stu_age`) as `最小年齡`, `c_id` as `班級(jí)` from stu_info group by `c_id`
-
having子句
語(yǔ)法: 分組查詢語(yǔ)句 having 條件表達(dá)式
是對(duì)分組查詢的結(jié)果集,再次進(jìn)行條件查詢
示例: 查詢平均年齡小于25歲的班級(jí)
select avg(`stu_age`) as `平均年齡`, `c_id` as `班級(jí)` from stu_info group by `c_id` having `平均年齡` < 25(結(jié)果集中的字段翔始。與原數(shù)據(jù)表無(wú)關(guān))
三. 子查詢語(yǔ)句
查詢對(duì)象不再是一張具體的表飒筑,而是另一條查詢語(yǔ)句的結(jié)果集,子查詢
語(yǔ)法
select 表名.字段 from (select 查詢語(yǔ)句) as 表名 where 查詢條件
示例:子查詢寫(xiě)在 外層查詢語(yǔ)句 “表” 的位置
select
s_table.*
from
(select `stu_id`,`stu_name`,`stu_age` from stu_info) as s_table
where
s_table.age > 32
示例2:子查詢語(yǔ)句寫(xiě)在where子句后面
查詢已經(jīng)分到當(dāng)前開(kāi)設(shè)課程的學(xué)生信息
select * from stu_info where c_id = 'c_002' or c_id = 'c_001' or c_id="c_003" or c_id="c_004";
select stu_info.* from stu_info
where
stu_info.c_id
in
(select `c_id` from stu_class)
- 子查詢書(shū)寫(xiě)位置绽昏, 表的位置
- 寫(xiě)在where 字段 in / not in (子查詢語(yǔ)句)
- 子查詢語(yǔ)句的列不能有重復(fù)列
- 子查詢語(yǔ)句寫(xiě)在表位置协屡,一定要通過(guò)as 子查詢結(jié)果集命名,外層查詢必須使用該名稱來(lái)查詢相應(yīng)字段
- 子查詢可以無(wú)限嵌套全谤,結(jié)果依然是一個(gè)結(jié)果集肤晓,是一張新表