DML语句select使用
select 独立使用情况 (不配合其他子句)
一般用来查询系统变量(参数)
需要把参数全部记住
select @@port;
select @@socket;
推荐下面这种
show variables like '%部分参数%'
调用系统函数
select version();
select now();
也可以调用自己定义的函数
select 配合其他子句使用(单表) 每个子句都可以单独使用
其他子句有(书写顺序)
from
where
group by
having
order by
limit
select 配合 from使用 相当于linux的cat查询一个表的数据不加别的条件
查询整表数据
select * from t1;
*代表所有的列,也可以把所有的列都写上 也可以查询部分列,性能没有保证 表大要加过滤条件(生产中要禁止使用)
select 配合 from + where 使用 类似于过滤匹配条件才会过滤出来
where = > < >= <= != between and in
查询中国所有的城市
select * from city where countrycode='CHN';
from 表名 where 条件是 countrycode=中国的
查询世界上人口小于100的国家
select * from city where population<100;
from 表名 where 条件是 population小于100的
查询世界上大于100万小于200万的国家
select * from city where population between 1000000 and 2000000;
from 表名 where条件 between大于等于1000000 and 小于等于20000000
查询中国和美国所有的城市
select * from city where countrycode in ('CHN','USA');
from 表名 where 条件 in交集 ('','')满足两个条件的都显示出来 还有 not in 满足条件的不显示
and or
查询中国广东省城市信息
select * from city where countrycode='CHN' and district='guangdong';
from 表名 where 条件1 and 条件2 需要瞒住两个条件才会显示
查询中国或美国城市信息
##查询中国或美国的城市信息
select * from world.city where countrycode='CHN' or countrycode='USA';
这个查询是中国和美国所有城市信息 索引设置好的话 in条件性能好 or要扫描表两次
like 条件 只能针对字符串列生效
查询国家代码是CH开头的,城市信息
select * from world.city where countrycode like 'CH%';
满足CH开头的国家城市都会打印出来
从语法角度可以CH可以前后都加% 前面不能加百分号 后面加百分号可以走索引 前后都加不走索引走全表扫描
group by语句
group by 一般配合 聚合函数使用 count() , sum() , avg(), max(), min(), group_concat()
统计每个国家有多少城市
select countrycode,count(*) from world.city group by countrycode;
1先from 表拿数据
2 然后 group by 分组
然后在count 加起来
统计每个国家的人口总数
select countrycode,sum(population) from world.city group by countrycode;
1先from 表拿数据
2然后 group by 分组
然后 sum 人口 加起来
统计每个国家有多少城市,并显示城市名
select countrycode,count(*) , group_concat(name) from world.city group by countrycode;
1先from 表拿数据
2然后按照国家代码分组
3先把每个国家有多少城市加起来 然后group_concat类似于列转换行 不然不符合sql_mode标准 要求要么在group by后
要么在聚合函数中
执行逻辑
select countrycode,count(*) ,group_concat(name) from world.city group by countrycode;
1会将原表数据页取出来
2按照 group by 条件升序排序
3针对 group by 分组,去重复 变成
4然后 把每个分组对应的数据行进行count(数一下组内有多少行)
5然后在group_concat 列转换成行显示按照,分割
统计city表的总行数
select count(*) from city;
说明聚合函数可以不配合group by 使用 但是group by 必须配合聚合函数使用
having使用 类似于where, 需要在group by 聚合函数后过滤(条件无法走索引)
统计每个国家的城市个数大于100显示
select countrycode,count(*) from world.city group by countrycode having count(*)>100;
order by使用 排序
统计每个国家的城市个数大于100显示并排序
select countrycode,count(*) from world.city group by countrycode having count(*)>100 order by count(*) ;
要按照子句顺序去写,子句可以单独使用
order by 默认是按照从小到大去排序 如果要按照从大到小 可以在条件后面加上desc
limit 一般是配合order by 一起使用才有意义
limit有两种写法
假如有10行数据
limit N offset H
limit 3 offset 5 跳过前5行显示6-8行数据
limit M,N
limit 5,3 跳过5行显示6-8行数据
统计世界上人口数最多的10个城市
select * from world.city order by population desc limit 10;
limit 10 显示前10个
select countrycode,sum(population) from world.city group by countrycode order by sum(population);
多表链接查询
写多表链接的关键需要找到交集列
查询 每个老师教了那些课程
1分析题目找到需要的表
teacher course
2找关联条件
teacher.tno = course.tno
3写出语句
select teacher.tname,course.cname from teacher join course on teacher.tno = course.tno;
查询张三 学习的课程的名
1先找出有关联的表
学生表 student
成绩表 sc
课程表 course
2找出关联条件
student.son
sc.son
sc.con
course.con
select student.sname,course.cname from student join sc on student.sno = sc.sno join course on sc.cno = course.cno where student.sname='zhang3';
最后要跟要你要查询人的的列=谁来做过滤条件
统计每个学生学习课程的门数
1这里有陷阱,需要两张表就行了需要学生表和成绩表(因为学生,学了这门课就会有成绩)
1 select student.sname,count(*) from student join sc on student.sno = sc.sno group by student.sno;
2 select student.sname,count(*) from student join sc on student.sno = sc.sno join course on sc.cno = course.cno group by student.sname;
统计每个学生学习课程的名称
1要找到有关联关系的表
2找出关联关系的列
3
select student.sname , group_concat(course.cname) from student join sc on student.sno = sc.sno join course on sc.cno = course.cno group by student.sno;
别名
select_list 别名 , 表别名
作用可以把列输入成你想要的的
select user as '用户' ,host as '白名单' from mysql.user;
可以把输出出来最上面的列输出成你想要的的
最后输出的时候才会调用
可以在聚合函数中使用因为聚合函数在group by 之后就调用了
表别名也可以设置到 from后 join后 整个语句就可以调用
子查询
常见
select from ()
select from join ()
select from where ()
括号里面的叫子查询
是先执行括号内部的查询 在执行货号外的查询
查询人口数小于100人的,国家名 国土面积
select code ,name ,surfacearea from country where code=(select countrycode from city where population<100);
union 和union all 的区别
union 会娶重复 需要使用临时表 对结果集进行排序去重
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。