select Sname,2021-Sage
from student;
select Sname,'出生月份:',2021-Sage,LOWER(Sdept)
from student;
这里还可以使用列的别名。具体用法就是在查询的列名后面加上那个别名,空格隔开。
例如:
大写函数:UPPER
关于all修饰和distinct修饰,之前提到过,distinct就是去掉重复的。
缺省为distinct。
select Sname
from student
where Sdept='CS';
select Sname
from student
where Sage<20;
select Sno
from SC
where Grade<60;
select Sname , Sdept,Sage
from student
where Sage between 18 and 23;
select Sname , Sdept,Sage
from student
where Sage not between 18 and 23;
谓词:IN <值表>, NOT IN <值表>
select Sname,Ssex
from student
where Sdept in ('cs','ma','is');
select Sname,Ssex
from student
where Sdept not in ('cs','ma','is');
谓词: [NOT] LIKE ‘<匹配串>’ [ESCAPE ‘ <换
码字符>’]
注意两点:
WHERE Cname LIKE 'DB\%Design' ESCAPE '\' ;
表示的是\后面的那个字符是真正的字符而不是通配字符。select *
from student
where Sno like '201215130';
这样这样写其实没什么意义(doge),直接Sno=就可
select Sname,Sno,Ssex
from student
where Sname like '梁%';
select Sname '姓名'
from student
where Sname like '欧阳_';
注意:关于编码问题,我们在c语言的时候就遇到过,当时一个汉字占两个字符,因为c语言是asc编码,而我们在学Java的时候,Java用的Unicode编码,所以每个汉字也是一个char(但这时char是两个字节),所以这里应该是Unicode或者gbk的。
然后是关于SQL sever里的char他是固定的长度,所以实际的“欧阳修”,由于是nchar(10),他在里面存的是 “欧阳修 ”,所以按理说 like’欧阳_'应该查询不成功,但是想必sql server做了处理。
下图,当like’欧阳_'或者’欧阳修___________'都可以查到’欧阳修’。
select Sname '姓名'
from student
where Sname like '_阳%'
select Sname '姓名',Sno '学号',Ssex '性别'
from student
where Sname not like '梁%'
select Cno , Ccredit
from course
where Cname like 'DB/_Design' escape '/';
select Cno , Ccredit
from course
where Cname like 'DB/_%i__' escape '/';
select Sno,Cno
from sc
where Grade is null;
“注意:IS” 不能用 “=” 代替
select Sno,Cno
from sc
where Grade is not null;
select Sname
from student
where Sdept='CS' AND Sage <20
select Sname
from student
where Sdept ='cs' OR Sdept ='ma' OR Sdept ='is'
注意:可以按照好几个属性排序,有优先级。(类似写的cmp,asc升序;desc降序,默认为asc);对于空值,由具体系统实现决定(也就是说没有规定)
select Sno,Grade
from sc
where Cno='3'
order by grade desc
select *
from student
order by Sdept ,Sage desc
常用函数
? 统计元组个数 COUNT(*)
? 统计一列中值的个数 COUNT([DISTINCT|ALL] <列名>)
? 计算一列值的总和 SUM([DISTINCT|ALL] <列名>)
? 计算一列值的平均值 AVG([DISTINCT|ALL] <列名>)
? 求一列中的最大值和最小值
MAX([DISTINCT|ALL] <列名>)
MIN([DISTINCT|ALL] <列名>)
select count(*)
from student ;
select count(distinct sno) '学生的个数'
from sc ;
select avg(grade)'平均分'
from sc
where cno=1;
select max(grade)'最高分'
from sc
where cno=1;
select sum(Ccredit)
from sc,course
where sc.cno=course.cno
关于这里还不太懂,应该是俩表连接了
细化聚集函数的作用对象
? 如果未对查询结果分组,聚集函数将作用于整个查询结果
? 对查询结果分组后,聚集函数将分别作用于每个组
? 按指定的一列或多列值分组,值相等的为一组
select Cno,count(sno) '每个课程里的学生的数目'
from sc
group by Cno;
select sno
from sc
group by Sno
having count (*) >2;
终于写完了,昨天写了一半。
关于许多细节,有很多不明白的地方。
详解 Spring注解的(ListMap)特殊注入功能 最近接手一个新项目,已经没有原开发...
在用到THINKPHP5的分页的时候,我们可以发现获取的数据是对象,如果我们要对数据...
定位问题的时候知识、经验是关键基础数据是依据工具是运用知识处理数据的手段。 ...
Kestrel Kestrel 是一个跨平台的适用于 ASP.NET Core 的 Web 服务器,默认包括在 ...
随着国际环境日趋复杂,数据安全与信息技术发展成为发展首要任务。目前,随着信...
文章目录 1. 大数加法 2. 大数幂运算 3.大数求余 废话不多说直接上代码了。 1. ...
文章目录 一、Zabbix Server 端配置 二、Web 页面配置监控机制 ①管理→报警媒介...
如何开始 C#学习过程中有一集讲的是如何查看类图,看完视频后自己就学习如何在VS...
本文实例为大家分享了ajax获取跨域数据的具体代码,供大家参考,具体内容如下 1....
引言 前几天 .NET Core3.1 发布,于是我把公司一个基础通用系统升级了,同时删除了...