**查询功能**
-
查询表:select * from
所需要查询的表; -
查询表中的元素:select
所需要查询的元素from被查询的表中; -
模糊查询
-
%:匹配任意个字符; 如:where from like '刘%'; -
_:匹配单个字符;如:where from like '_辰';
查询全体学生的姓名,学号;
select sno,sname from student;
–查询全体学生的姓名、学号、所在系;
``
select sname,sno,sdept from student;
–查询全体学生的详细记录
select sname,ssex,sno,sage,sdept from student;
–查询全体学生的姓名及其出生年份;
select sname,2019-sage 出生年份 from student;
–查询全体学生的姓名、出生年份和所在系,要求用小写字母表示系
select sname,2019-sage 出生年份,lower(sdept) from student;
–查询选修了课程的学生学号;
select distinct sno from sc; --distinct的作用是去掉重复的数据
–查询所有计算机科学系的全体学生的名单
select sname,sage from student where sdept='CP';
–查询所有年龄在20以下的学生姓名及其年龄
select sname,sage from student where sage<20;
--查询年龄在20··~23之间的学生的姓名、系别、年龄
select sname,sdept,sage from student where sage>=20 and sage<=23;
select sname, sdept,sage from student where sage not between 20 and 23;
–查询物理系(EL)、人文系(YC)、和计算机科学系(CP)学生的姓名和性别;
select sname,sage from student where sdept='EL' or sdept='YC' or sdept='CP';
select sname ,sage from student where sdept in ('EL','YC','CP');
–查询不是物理系(EL)、人文系(YC)、和计算机科学系(CP)学生的姓名和性别;
select sname,sage from student where not(sdept='EL' or sdept='YC' or sdept='CP') ;
select sname ,sage from student where sdept not in ('EL','YC','CP');
–查询学号为12308的学生的详细情况;
select * from student where sno='12308';
select * from student where sno like '12308';
select * from student where sname ='小泳';
select * from student where sname like '小泳 ';--like和’=‘的区别:=会自动补齐字符长度,而like需要自行补足字符长度
desc student;--查询学生表中各元素的字符长度
--查询所有姓小学生的姓名、学号、和性别;
select sname,sno,ssex from student where sname like '小%';
select sname,sno,ssex from student where sname like '李_%';
desc student;
博客主要展示了一系列SQL查询语句示例,包括查询表、表中元素、模糊查询等,还涉及查询学生的姓名、学号、所在系、年龄等信息,以及去重、特定条件筛选等操作,涵盖多种常见的SQL查询场景。
35万+

被折叠的 条评论
为什么被折叠?



