一、单表查询
1、select
select @@xxx; 获取参数信息。
结尾\G竖直显示
show variables 查看MySQL中所有的参数。
2、select语句的执行顺序(单表)
slect 开始
from
where 条件匹配
group by 分组
select之后的语句
having 在group by 后继续匹配
order by 排序(desc)降序
limit 分页显示
3、多表连接查询
传统连接:
基于where条件,思路为,找表之间的关系(管系列),排列查询条件。
内连接:join on
1.找表之间的关系列
2.将两表放在join左右
3.将关联条件放在on后
4.将所有的查询条件进行罗列
select A.a,B.b
from
A join B
on A.x=B.y
where
group by
order by
limit
group by必须做到唯一
MySQL5.7版本开启了group by 严格,因此当group by 出现不唯一的可能性比如没有主键列时,会报错。
二、习题
1.查询中国城市信息(显示前五行)
mysql> select * from world.city where countrycode='CHN' limit 5;
+------+-----------+-------------+-----------+------------+
| ID | Name | CountryCode | District | Population |
+------+-----------+-------------+-----------+------------+
| 1890 | Shanghai | CHN | Shanghai | 9696300 |
| 1891 | Peking | CHN | Peking | 7472000 |
| 1892 | Chongqing | CHN | Chongqing | 6351600 |
| 1893 | Tianjin | CHN | Tianjin | 5286800 |
| 1894 | Wuhan | CHN | Hubei | 4344600 |
+------+-----------+-------------+-----------+------------+
2.查询美国的城市信息(显示五行)
mysql> select * from world.city where countrycode='USA' limit 5;
+------+--------------+-------------+--------------+------------+
| ID | Name | CountryCode | District | Population |
+------+--------------+-------------+--------------+------------+
| 3793 | New York | USA | New York | 8008278 |
| 3794 | Los Angeles | USA | California | 3694820 |
| 3795 | Chicago | USA | Illinois | 2896016 |
| 3796 | Houston | USA | Texas | 1953631 |
| 3797 | Philadelphia | USA | Pennsylvania | 1517550 |
+------+--------------+-------------+--------------+------------+
3.查询世界上人口小于100的城市
vmysql> select * from world.city where population<100;
+------+-----------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+-----------+-------------+----------+------------+
| 2912 | Adamstown | PCN | – | 42 |
+------+-----------+-------------+----------+------------+
4.查询城市人口在1w到2w之间的城市(前五)
mysql> select * from world.city where population>10000 and population<20000 limit 5;
+-----+-------------+-------------+--------------+------------+
| ID | Name | CountryCode | District | Population |
+-----+-------------+-------------+--------------+------------+
| 553 | George Town | CYM | Grand Cayman | 19600 |
| 583 | Avarua | COK | Rarotonga | 11900 |
| 586 | Roseau | DMA | St George | 16243 |
| 901 | Tórshavn | FRO | Streymoyar | 14542 |
| 917 | Nuuk | GRL | Kitaa | 13445 |
+-----+-------------+-------------+--------------+------------+
或
mysql> select * from world.city where population between 10000 and 200000 limit 5;
+----+----------------+-------------+---------------+------------+
| ID | Name | CountryCode | District | Population |
+----+----------------+-------------+---------------+------------+
| 3 | Herat | AFG | Herat | 186800 |
| 4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
| 10 | Tilburg | NLD | Noord-Brabant | 193238 |
| 11 | Groningen | NLD | Groningen | 172701 |
| 12 | Breda | NLD | Noord-Brabant | 160398 |
+----+----------------+-------------+---------------+------------+
5.查询中国或美国的城市信息(前五)
mysql> select * from world.city where CountryCode='CHN' or CountryCode='USA' limit 5;
+------+-----------+-------------+-----------+------------+
| ID | Name | CountryCode | District | Population |
+------+-----------+-------------+-----------+------------+
| 1890 | Shanghai | CHN | Shanghai | 9696300 |
| 1891 | Peking | CHN | Peking | 7472000 |
| 1892 | Chongqing | CHN | Chongqing | 6351600 |
| 1893 | Tianjin | CHN | Tianjin | 5286800 |
| 1894 | Wuhan | CHN | Hubei | 4344600 |
+------+-----------+-------------+-----------+------------+
6.查询city表中所有数据(极少用)
mysql> select * from world.city limit 5;
+----+----------------+-------------+---------------+------------+
| ID | Name | CountryCode | District | Population |
+----+----------------+-------------+---------------+------------+
| 1 | Kabul | AFG | Kabol | 1780000 |
| 2 | Qandahar | AFG | Qandahar | 237500 |
| 3 | Herat | AFG | Herat | 186800 |
| 4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
| 5 | Amsterdam | NLD | Noord-Holland | 731200 |
+----+----------------+-------------+---------------+------------+
7.查询name和population的所有值(前五)
mysql> select name,Population from world.city limit 5;
+----------------+------------+
| name | Population |
+----------------+------------+
| Kabul | 1780000 |
| Qandahar | 237500 |
| Herat | 186800 |
| Mazar-e-Sharif | 127800 |
| Amsterdam | 731200 |
+----------------+------------+
8.统计每个国家的总人数(前五)
mysql> select CountryCode,sum(Population) from world.city group by CountryCode limit 5;
+-------------+-----------------+
| CountryCode | sum(Population) |
+-------------+-----------------+
| ABW | 29034 |
| AFG | 2332100 |
| AGO | 2561600 |
| AIA | 1556 |
| ALB | 270000 |
+-------------+-----------------+
9.统计每个国家的城市个数(前五)
mysql> select countrycode,count(id) from world.city group by countrycode limit 5;
+-------------+-----------+
| countrycode | count(id) |
+-------------+-----------+
| ABW | 1 |
| AFG | 4 |
| AGO | 5 |
| AIA | 2 |
| ALB | 1 |
+-------------+-----------+
10.统计中国每个省的城市列表
mysql> select district,group_concat(name) from world.city where countrycode='CHN'group by district limit 5;
+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| district | group_concat(name) |
+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Anhui | Hefei,Huainan,Bengbu,Wuhu,Huaibei,Ma´anshan,Anqing,Tongling,Fuyang,Suzhou,Liu´an,Chuzhou,Chaohu,Xuangzhou,Bozhou,Huangshan |
| Chongqing | Chongqing |
| Fujian | Fuzhou,Amoy [Xiamen],Nanping,Quanzhou,Zhangzhou,Sanming,Longyan,Yong´an,Fu´an,Fuqing,Putian,Shaowu |
| Gansu | Lanzhou,Tianshui,Baiyin,Wuwei,Yumen,Jinchang,Pingliang |
| Guangdong | Kanton [Guangzhou],Shenzhen,Shantou,Zhangjiang,Shaoguan,Chaozhou,Dongwan,Foshan,Zhongshan,Jiangmen,Yangjiang,Zhaoqing,Maoming,Zhuhai,Qingyuan,Huizhou,Meixian,Heyuan,Shanwei,Jieyang |
+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
11.统计中国每个省的总人口数
mysql> select district,sum(population) from world.city where countrycode='CHN' group by district limit 5;
+-----------+-----------------+
| district | sum(population) |
+-----------+-----------------+
| Anhui | 5141136 |
| Chongqing | 6351600 |
| Fujian | 3575650 |
| Gansu | 2462631 |
| Guangdong | 9510263 |
+-----------+-----------------+
12.统计中国每个省的平均人口
mysql> select district,avg(population) from world.city where countrycode='CHN' grroup by district limit 5;
+-----------+-----------------+
| district | avg(population) |
+-----------+-----------------+
| Anhui | 321321.0000 |
| Chongqing | 6351600.0000 |
| Fujian | 297970.8333 |
| Gansu | 351804.4286 |
| Guangdong | 475513.1500 |
+-----------+-----------------+
13.统计中国每个省大于1000W的省及人口数
mysql> select district,sum(population) from world.city where countrycode='CHN' group by district having sum(population)>10000000;
+--------------+-----------------+
| district | sum(population) |
+--------------+-----------------+
| Heilongjiang | 11628057 |
| Liaoning | 15079174 |
| Shandong | 12114416 |
+--------------+-----------------+
14.降序排列中国每个省大于1000W的省及人口数
mysql> select district,sum(population) from world.city where countrycode='CHN' group by district having sum(population)>10000000 order by sum(population);
+--------------+-----------------+
| district | sum(population) |
+--------------+-----------------+
| Heilongjiang | 11628057 |
| Shandong | 12114416 |
| Liaoning | 15079174 |
+--------------+-----------------+
三、多表查询
1.查询世界上人口数量小于100的国家,城市名,国土面积
mysql> SELECT country.name,city.name,country.surfacearea
-> FROM
-> city JOIN country
-> ON city.countrycode=country.code
-> WHERE city.population<100;
+----------+-----------+-------------+
| name | name | surfacearea |
+----------+-----------+-------------+
| Pitcairn | Adamstown | 49.00 |
+----------+-----------+-------------+
2.查询oldguo老师和他的课程
on course.tno=teacher.tno where teacher.tname='oldguo';
+--------+-------+
| tname | cname |
+--------+-------+
| oldguo | mysql |
+--------+-------+
3.统计没门课程的总成绩
mysql> select course.cname,sum(sc.score)
-> from student
-> join sc
-> on student.sno=sc.sno
-> join course
-> on sc.cno=course.cno
-> group by course.cno;
+--------+---------------+
| cname | sum(sc.score) |
+--------+---------------+
| linux | 484 |
| python | 210 |
| mysql | 614 |
+--------+---------------+
4.查询oldguo老师教的学生姓名列表
mysql> select teacher.tname,group_concat(student.sname)
-> from student
-> join sc
-> on student.sno=sc.sno
-> join course
-> on course.cno=sc.cno
-> join teacher
-> on teacher.tno=course.tno
-> where teacher.tname='oldguo'
-> group by teacher.tno;
+--------+---------------------------------------------+
| tname | group_concat(student.sname) |
+--------+---------------------------------------------+
| oldguo | zhang4,li4,wang5,zh4,zhao4,ma6,oldgirl,oldp |
+--------+---------------------------------------------+
5.查询所有老师教的学生姓名列表
mysql> select teacher.tname,group_concat(student.sname) from student join sc on student.sno=sc.sno join course on course.cno=sc.cno join teacher on teacher.tno=course.tno group by teacher.tno;
+--------+---------------------------------------------+
| tname | group_concat(student.sname) |
+--------+---------------------------------------------+
| oldboy | zhang3,li4,wang5,zhao4,ma6,oldboy |
| hesw | zhang3,zhang4,wang5 |
| oldguo | zhang4,li4,wang5,zh4,zhao4,ma6,oldgirl,oldp |
+--------+---------------------------------------------+
6.查询oldguo老师所教不及格的学生姓名
mysql> select teacher.tname,group_concat(student.sname)
-> from student
-> join sc
-> on sc.sno=student.sno
-> join course
-> on course.cno=sc.cno
-> join teacher
-> on teacher.tno=course.tno
-> where teacher.tname='oldguo' and sc.score<60
-> ;
+--------+-----------------------------+
| tname | group_concat(student.sname) |
+--------+-----------------------------+
| oldguo | li4,zh4 |
+--------+-----------------------------+
7.统计zhang3学了几门课
mysql> select student.sname,count(sc.sno) from student join sc on sc.sno=student.sno join course on course.cno=sc.cno where student.sname='zhang3' group by sc.snno;
+--------+---------------+
| sname | count(sc.sno) |
+--------+---------------+
| zhang3 | 2 |
+--------+---------------+
8.查询zhang3学习的课程有哪些
mysql> select student.sname,group_concat(course.cname) from student join sc on scc.sno=student.sno join course on course.cno=sc.cno where student.sname='zhang3';
+--------+----------------------------+
| sname | group_concat(course.cname) |
+--------+----------------------------+
| zhang3 | linux,python |
+--------+----------------------------+
9.查询oldboy老师教的学生列表
mysql> select teacher.tname,group_concat(student.sname) from student join sc on sc.sno=student.sno join course on course.cno=sc.cno join teacher on teacher.tno=course.tno where teacher.tname='oldboy' group by teacher.tno;
+--------+-----------------------------------+
| tname | group_concat(student.sname) |
+--------+-----------------------------------+
| oldboy | zhang3,li4,wang5,zhao4,ma6,oldboy |
+--------+-----------------------------------+
10.查询oldguo所教课程平均分
mysql> select teacher.tname,avg(sc.score) from student join sc on sc.sno=student..sno join course on course.cno=sc.cno join teacher on teacher.tno=course.tno wherre teacher.tname='oldguo' group by course.cno;
+--------+---------------+
| tname | avg(sc.score) |
+--------+---------------+
| oldguo | 76.7500 |
11.每位老师所教课程的平均分,并降序排列
mysql> select teacher.tname,avg(sc.score) from student join sc on sc.sno=student.sno join course on course.cno=sc.cno join teacher on teacher.tno=course.tno group by teacher.tno order by avg(sc.score) desc;
+--------+---------------+
| tname | avg(sc.score) |
+--------+---------------+
| oldboy | 80.6667 |
| oldguo | 76.7500 |
| hesw | 70.0000 |
+--------+---------------+
12.查询oldguo所教学生不及格的姓名
mysql> select teacher.tname,group_concat(student.sname) from student join sc on sc.sno=student.sno join course on course.cno=sc.cno join teacher on teacher.tno=ccourse.tno where teacher.tname='oldguo' and sc.score<60;
+--------+-----------------------------+
| tname | group_concat(student.sname) |
+--------+-----------------------------+
| oldguo | li4,zh4 |
+--------+-----------------------------+
13.查询所有老师所教学生不及格信息
mysql> select teacher.tname,group_concat(student.sname) from student join sc on sc.sno=student.sno join course on course.cno=sc.cno join teacher on teacher.tno=course.tno where sc.score<60 group by teacher.tno;
+--------+-----------------------------+
| tname | group_concat(student.sname) |
+--------+-----------------------------+
| hesw | zhang3 |
| oldguo | li4,zh4 |
+--------+-----------------------------+
四、总结
1.模糊查询中like语句在MySQL中不要出现%在前面的情况,因为当有大量数据时,效率极低。
2.对于合并表
union 合并两个表,若其中有重复,将自动去重
union all 合并两个表,不去重,因此性能最好
select * from xxx; 适合表数据较少情况下
group by 配合聚合函数应用
常用聚合函数:
avg() 平均
count() 计算个数
sum() 总数
max() 最大
min() 最小
group_concat() 集中显示所有
group by 后接归类条件
select 后接查看条件
where 后接查询条件
having: guoup by 后过滤
显示统计完的某一部分数据
order by 排序
desc 降序
limit: 分页显示