1檩小、select
1.1 作用
獲取mysql中的數(shù)據(jù)行
1.2 單獨(dú)使用select
1) select @@xxxx; 獲取參數(shù)信息
select @@prot;
down variables like '%innodb%'
2) select 函數(shù)()
select database();
select now();
select version();
1.3 SQL92標(biāo)準(zhǔn)的使用語法
1) select語法執(zhí)行順序(單表)
select開始--->from子句--->where子句--->group by子句(select之后的執(zhí)行條件)--->having子句--->order by--->limit
2) select 語句應(yīng)用
1)from
例子:查詢city表中的所有數(shù)據(jù)
use world;
select * from city; --->適合表數(shù)據(jù)行較少的验毡,生產(chǎn)中使用較少的
select * from world city;
例子:查詢name和population所有值
select name,population from city;
select name,population from world city;
例子:?jiǎn)伪聿樵兙毩?xí)環(huán)境,world數(shù)據(jù)庫(kù)下的介紹
show tables from world;
city(城市):
--------------------
desc city;
id:自增的無關(guān)列卦碾,數(shù)據(jù)行的需要
name:城市名字
countrycode:城市所在的國(guó)家代號(hào)图云,CHN,USA,JPN...
district:城市的所在區(qū)域,中國(guó)是省的意思卤材,美國(guó)是洲的意思
population:城市的人口數(shù)量
說明:此表示歷史數(shù)據(jù)遮斥。
country(國(guó)家)
countrylanguage(國(guó)家語言)
--------------------
剛?cè)肼毜腄BA熟悉業(yè)務(wù):
DBA剛?cè)肼殨r(shí),DBA的任務(wù)
1扇丛、搞清楚架構(gòu)
通過公司架構(gòu)圖术吗,搞清楚數(shù)據(jù)庫(kù)的物理架構(gòu)
1-2天
邏輯結(jié)構(gòu):
1)生產(chǎn)庫(kù)信息(容易達(dá)到)
2)庫(kù)下表信息(非常復(fù)雜)
a、開發(fā)和業(yè)務(wù)人員打好關(guān)系
b帆精、搞到ER圖(PD)
c较屿、啥都沒有咋整?
1)卓练、找到建表語句隘蝎,如果有注釋,讀懂注釋就可以了襟企。如果沒有注釋嘱么,目前只能根據(jù)列名翻譯
2)、找到表中部分?jǐn)?shù)據(jù)顽悼,分析數(shù)據(jù)特點(diǎn)曼振,達(dá)到了解列功能的一個(gè)目的
2)where
where 配合 等值查詢
查詢中國(guó)的城市信息
select * from world.city where countrycode='CHN';
查詢美國(guó)的城市信息
select * from world.city where countrycode='USA';
where 配合 不等值 (> < >= <= <>)
查詢世界上人口小于100的城市
select * from world.city where population<100;
查詢世界上人口大于10000000城市
select * from world.city where Population>10000000;
--------------------------
where 配合 模糊(like)
查詢國(guó)家代號(hào)是C開頭的城市
select * from world.city where countrycode like 'C%';
注意:like語句在MySQL中几迄,不要出現(xiàn)%在前面的清空。因此效率很低冰评,不走索引
--------------------------
where 配合 邏輯連接符(AND OR)
查詢城市人口在10000和20000之間的
select * from world.city where population > 10000 and population < 20000;
查詢中國(guó)或美國(guó)的的城市信息
select * from world.city where countrycode='CHN' or countrycode='USA'
用union all來實(shí)現(xiàn)
select * from world.city where countrycode='CHN' union all select * from world.city where countrycode='USA';
3)group by
group by 配合聚合函數(shù)應(yīng)用
常用的聚合函數(shù):
avg () 平均數(shù)
count () 統(tǒng)計(jì)個(gè)數(shù)
sum () 統(tǒng)計(jì)總數(shù)
max () 最大數(shù)
min () 最小數(shù)
group_concat () 統(tǒng)計(jì)列表
-------------------------
統(tǒng)計(jì)每個(gè)國(guó)家的總?cè)丝跀?shù)
select countrycode,sum(population) from world.city group by countrycode;
統(tǒng)計(jì)每個(gè)國(guó)家的城市個(gè)數(shù)
select countrycode,count(id) from world.city group by countryCode;
統(tǒng)計(jì)每個(gè)國(guó)家的省的個(gè)數(shù)
select countrycode,group_concat(district) from world.city group by countrycode;
統(tǒng)計(jì)中國(guó)每個(gè)省的城市名列表
select district,group_concat(name) from world.city where countrycode='CHN' group by district;
統(tǒng)計(jì)中國(guó)每個(gè)省的總?cè)藬?shù)
SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district;
4)select
5)having 和Linux中的管道一樣映胁。
統(tǒng)計(jì)中國(guó),每個(gè)省的總?cè)丝诖笥?0000000萬的人數(shù)
SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district having sum(population) >10000000;
6)order by 后面加desc是從大到小集索,不加則從小到大屿愚。
統(tǒng)計(jì)中國(guó),每個(gè)省的總?cè)藬?shù)從小到大排序
SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district order by sum(population);
統(tǒng)計(jì)中國(guó)务荆,每個(gè)省的總?cè)藬?shù)從大到小排序
SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district order by sum(population) desc;
統(tǒng)計(jì)中國(guó)妆距,每個(gè)省的總?cè)藬?shù)從小到大排序
SELECT * FROM world.city WHERE countrycode = 'CHN' order by population desc;
7)limit m,n 跳過m行,顯示n行
limit m offset n 跳過m行函匕,顯示n行
1.4 多表連接查詢
1) 介紹4張測(cè)試表的關(guān)系
2) 什么時(shí)候用娱据?
需要查詢的數(shù)據(jù)是來自于多張表時(shí)。
3) 怎么去多表連接查詢
傳統(tǒng)的連接:基于where條件
1盅惜、找表之間的關(guān)系列
2中剩、排列查詢條件
需求:人口數(shù)量小于100人的城市,所在國(guó)家的國(guó)土面積(城市名抒寂,國(guó)家名结啼,國(guó)土面積)
select name,countrycode from city where population<100; 得到PCN
select name,surfacearea from country where code='PCN';
上面命令合為下邊的一條:
select city.name,country.name,country.surfacearea from city,country where city.countrycode = country.code and city.population<100;
----------------
內(nèi)連接
1、找表之間的關(guān)系列
2屈芜、將倆表放在join左右
3郊愧、將關(guān)聯(lián)條件了放在on后面
4、將所有的查詢條件進(jìn)行羅列井佑。
---------------
例子:
1属铁、查詢?nèi)丝跀?shù)量小于100人的國(guó)家名。
select country.name,city.name,country.SurfaceArea from city join country on city.countrycode=country.code where city.population<100;
2躬翁、查詢oldguo老師和他教的課程名稱焦蘑。
select teacher.tname,course.cname from teacher join course on teacher.tno=course.tno where teacher.tname='oldguo';
3、查詢oldguo老師教的學(xué)生姓名列表
select teacher.tname,group_concat(student.sname)
from teacher
join course
on teacher.tno=course.tno
join score
on course.cno=score.cno
join student
on score.sno=student.sno
where teacher.tname='oldguo';
6盒发、查詢oldboy老師教的不及格學(xué)生的姓名
select teacher.tname,group_concat(student.sname)
from teacher
join course
on teacher.tno=course.tno
join score
on course.cno=score.cno
join student
on score.sno=student.sno
where teacher.tname='oldguo' and score.score <60
group by student.sname;
7例嘱、 統(tǒng)計(jì)zhang3,學(xué)習(xí)了幾門課
select student.sname,sum(score.sno)
from student
join score
on student.sno=score.sno
join course
on score.cno=course.cno
where student.sname='zhang3' and student.sno
group by student.sno;
8、 統(tǒng)計(jì)zhang3,學(xué)習(xí)了幾門課
select student.sname,course.cname
from student
join score
on student.sno=score.sno
join course
on score.cno=course.cno
where student.sname='zhang3';
9宁舰、查詢oldguo老師教的學(xué)生名.
select teacher.tname,student.sname
from teacher
join course
on teacher.tno=course.tno
join score
on course.cno=score.cno
join student
on score.sno=student.sno
where teacher.tname='oldguo';
10蝶防、查詢oldguo所教課程的平均分?jǐn)?shù)
select teacher.tname,avg(score.score)
from teacher
join course
on teacher.tno=course.tno
join score
on course.cno=score.cno
where teacher.tname='oldguo';
11.每位老師所教課程的平均分,并按平均分排序
select teacher.tname,avg(score.score)
from teacher
join course
on teacher.tno=course.tno
join score
on course.cno=score.cno
group by teacher.tno
order by avg(score.score);
12.查詢oldguo所教的不及格的學(xué)生姓名
select teacher.tname,group_concat(student.sname)
from teacher
join course
on teacher.tno=course.tno
join score
on course.cno=score.cno
join student
on score.sno=student.sno
where teacher.tname='oldguo' and score.score <60
group by student.sname;
13.查詢所有老師所教學(xué)生不及格的信息
select teacher.tname,student.sname,score.score
from teacher
join course
on teacher.tno=course.tno
join score
on course.cno=score.cno
join student
on score.sno=student.sno
where score.score <60
----------------------------------
mysql> SELECT course.cno,course.cname,SUM(sc.score)
-> FROM course
-> JOIN sc
-> ON course.cno = sc.cno
-> GROUP BY course.cname;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'school.course.cno' which
is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
1. 在select后面出現(xiàn)的列,不是分組條件明吩,并且沒有在函數(shù)中出現(xiàn)间学。
2. 如果group by 后是主鍵列或者是唯一條件列。如下:
SELECT
course.cno,course.cname,SUM(sc.score) FROM course
JOIN sc
ON course.cno = sc.cno
GROUP BY course.cno;
-------------------------------------
2、show
3低葫、Information_schema
--- 6. 查詢oldboy老師教的不及格學(xué)生的姓名
--- 7. 統(tǒng)計(jì)zhang3,學(xué)習(xí)了幾門課
--- 8. 查詢zhang3,學(xué)習(xí)的課程名稱有哪些?
--- 9. 查詢oldguo老師教的學(xué)生名.
--- 10.查詢oldguo所教課程的平均分?jǐn)?shù)
--- 11.每位老師所教課程的平均分,并按平均分排序
--- 12.查詢oldguo所教的不及格的學(xué)生姓名
--- 13.查詢所有老師所教學(xué)生不及格的信息