SQL语句的高级操作实际训练
以下为所用的知识点
数据分组:
group by
having(用在group by 后)
数据排序:
正序:order by 字段
倒序:order by 字段 desc
多表联查:
公式:select * from 表1 join 表2 on 表1.字段1 = 表2.字段2 where 表1.字段1=值;
判断符:
注意:in/is/like/between 可以加 not 取反意
=
<
>
!= 不等于
in 表示在什么什么里面 后面要加括号()
is 查询是否为null
like 用于字符串的查询
between 表示在两者之前 查询数字的时候用
逻辑连接符:
注意:and/or可以加 not 取反义
and 并且
or 或者
聚合函数如下
count 统计数
max 最大值
min 最小值
avg 平均值
sum 求和
练习题如下图
根据题目要求做好两张表 ↓
表1:cust
表2:mark
第一题答案为
select avg(b.english) from cust a join mark b on a.studentno = b.studentno;
所得结果为
第二题答案为
select b.math,a.`name`,a.address,a.telno from cust a join mark b on a.studentno = b.studentno where a.studentno in(11,22,33,44,55);
所得结果为
第三题答案为
select a.`name`,b.computer from cust a join mark b on a.studentno = b.studentno order by b.computer desc;
所得结果为
第四题答案为
select a.studentno,a.`name`,sum(b.english+b.math+b.computer) zcj from cust a join mark b on a.studentno = b.studentno GROUP BY a.studentno HAVING zcj > 240 order by zcj desc;
所得结果为