- 數(shù)據(jù)準(zhǔn)備
number | name | age | class | grade |
---|---|---|---|---|
201804001 | 劉一 | 16 | 19 | 二年級(jí) |
201804002 | 陳二 | 19 | 18 | 一年級(jí) |
201804003 | 張三 | 20 | 19 | 二年級(jí) |
201804004 | 李四 | 17 | 19 | 一年級(jí) |
201804005 | 王五 | 18 | 19 | 三年級(jí) |
201804006 | 趙六 | 24 | 18 | 二年級(jí) |
201804007 | 孫七 | 22 | 19 | 三年級(jí) |
201804008 | 周八 | 21 | 19 | 二年級(jí) |
201804009 | 吳九 | 25 | 18 | 一年級(jí) |
201804010 | 鄭十 | 23 | 19 | 一年級(jí) |
201804011 | 小周周 | 20 | 18 | 二年級(jí) |
201804012 | 周周周 | 21 | 19 | 三年級(jí) |
篩選條件
- 比較運(yùn)算符
等于:=
大于等于:>=
小魚等于:<=
大于:>
小于:<
不等于:!=或<>
IS NULL
IS NOT NULL - 邏輯運(yùn)算符
與:and
或:or
非:not
查找16到20歲的學(xué)生select columns from table_name where age>=16 and age<=20;
- 其他操作
排序(order by)SELECT columns FROM tb_name ORDER BY columns desc;
正序:asc(默認(rèn))倒序:desc
限制個(gè)數(shù)(limit)SELECT columns FROM tb_name LIMIT start, count ;
或LIMIT count;
去重(distinct)SELECT DISTINCT columns FROM tb_name;
模糊查詢(like'%')select columns from table_name where name like '%周_';
注:%
表示任意多個(gè)字符泰涂;_
表示任意一個(gè)字符 - 范圍查詢
連續(xù)范圍: BETWEEN a AND b
select columns from table_name where age BETWEEN 16 and 20;
間隔返回: IN
select columns from table_name where column in(X,X,X);
聚合與分組
- 常用聚合函數(shù)
統(tǒng)計(jì)個(gè)數(shù):COUNT(column)select count(name) from student;
求和:SUM(column)
最大值:MAX(column)
平均值:AVG(column)
最小值:MIN(column)
列出字段全部值:GROUP_CONCAT(column)select group_concat(age) from student;
- 分組查詢(group by)
Select 字段 from 表 group by 字段;select class from student group by class;
Select 字段鲫竞,count(*) from 表 group by 字段;select class,count(*) from student group by class;
在分組的情況下,只能夠出現(xiàn)分組字段和聚合字段逼蒙,其他的字段沒有意義从绘,會(huì)報(bào)錯(cuò)! - 聚合篩選(having)
select class from student group by class,age having age>=18;
加having條件表達(dá)式是牢,可以對(duì)輸出的結(jié)果做出限制僵井,having有分組的作用
假如說一個(gè)查詢語句中同時(shí)包含了別名(as),聚合函數(shù)驳棱, where, having
select * from (select class,count(*) from student where age >=20 group by class,age having age>=21) as b;
統(tǒng)計(jì)年齡大于等于20歲的不同班級(jí)的學(xué)生的數(shù)量,對(duì)于統(tǒng)計(jì)的結(jié)果再統(tǒng)計(jì)年齡大于等于21的學(xué)生批什,把年齡相同的學(xué)生歸類到一起,最后把結(jié)果命名為表b展示出來
那么他們的執(zhí)行順序是
先是執(zhí)行:where
然后執(zhí)行:聚合函數(shù)和別名
最后執(zhí)行:having
子查詢(了解)
將一個(gè)查詢的結(jié)果留下來用于下一次查詢 ( select 中嵌套 select )
鏈接查詢(了解)
- 內(nèi)連接(inner join)
- 無條件內(nèi)連接:
無條件內(nèi)連接蹈胡,又名交叉連接/笛卡爾連接
第一張表種的每一項(xiàng)會(huì)和另一張表的每一項(xiàng)依次組合
Mysql> select * from student [inner] join scoren - 有條件內(nèi)連接:
在無條件內(nèi)鏈接的基礎(chǔ)上渊季,加上一個(gè)on子句
當(dāng)連接的時(shí)候朋蔫,篩選出那些有實(shí)際意義的記錄來進(jìn)行組合
Mysql> select * from student inner join scoren
-> on dept_id = id;
- 無條件內(nèi)連接:
- 外連接({left | right} join)
- 左外連接: (以左表為基準(zhǔn))
兩張表做連接的時(shí)候,在連接條件不匹配的時(shí)候
留下左表中的數(shù)據(jù)却汉,而右表中的數(shù)據(jù)以NULL填充
mysql> select * from student left join department
-> on dept_id= d_id; - 右外連接: (以右表為基準(zhǔn))
對(duì)兩張表做連接的時(shí)候驯妄,在連接條件不匹配的時(shí)候
留下右表中的數(shù)據(jù),而左表中的數(shù)據(jù)以NULL填充
mysql> select * from student right join department
-> on dept_id= d_id;
- 左外連接: (以左表為基準(zhǔn))
作業(yè)
從課堂上演示的students表里面
統(tǒng)計(jì)出所有人數(shù)
統(tǒng)計(jì)出age大于18的人數(shù)
統(tǒng)計(jì)出學(xué)python的人數(shù)
統(tǒng)計(jì)出學(xué)java的age大于18的人數(shù)