书写顺序
select distinct * from ‘表名’ where ‘限制条件’ group by ‘分组依据’ having ‘过滤条件’ order by limit ‘展示条数’
执行顺序
from – 查询
where – 限制条件
group by – 分组
having – 过滤条件
order by – 排序
limit – 展示条数
distinct – 去重
select – 查询的结果
正则:select * from emp where name regexp ‘^j.*(n|y)$’;
集合查询:max 、min 、avg 、sum 、count 、group_concat 。
内连接:inner join
左连接:left join
右连接:right join
全连接: 左连接 union 右连接
replace 替换
拼接:concat、concat_ws、group_concat
查询语句
select * from 表名 where 范围 – 选择查询
insert into 表名(field1,field2) values(value1,value2) – 插入
delete from 表名 where 范围 – 删除
update 表名 set field1=value1 where 范围 – 更新
select * from 表名 where field1 like ’%value1%’ – 查找
select * from 表名 order by field1,field2 [desc] – 排序:
select count as 需要统计总数的字段名 from 表名 – 总数
select sum(field1) as sumvalue from 表名 – 求和
select avg(field1) as avgvalue from 表名 – 平均
select max(field1) as maxvalue from 表名 – 最大
select min(field1) as minvalue from 表名 – 最小
查询实例
MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测
原创zhimadiandianyuan 最后发布于2019-09-19 14:01:05 阅读数 6975 收藏
展开
建表可自行百度,重点为查询;
一、数据准备:
1.student学生表数据:
2.teacher老师表数据:
3.course课程表数据:
4.score成绩表数据:
二、数据查询
1.查询平均成绩大于60分的同学的学生编号和学生姓名和平均成绩:
语句:select st.StuId,st.StuName as st_name,avg(sc.StuScore) from score as sc,student as st where st.StuId = sc.StuId group by st.StuId having avg(sc.StuScore) > 60;
结果:
2.查询所有学生的学号、姓名、选课数、总成绩
语句:select st.StuId,st.StuName,count(sc.StuId),sum(sc.Stuscore) from student as st,score as sc where