SQL——基础查询
创始人
2024-03-20 03:15:43
0

查看列

查看特定列

select 姓名 from 学生表
select 姓名,性别,家庭住址 from 学生表

查看所有列

select * from 学生表

使用计算列

求和

select sum(price) from fruit

求个数

select count(price) from fruit

求价格提升

select price*1.05 from fruit

求库存数量

select (进货数量-销售数量)as 库存数量 from fruit

使用distinct

作用于单列

select distinct 班级 from student

作用于多列

select distinct 班级,姓名 from student
#distinct 作用覆盖班级+姓名,是根据班级+姓名两个字段同时去重。并且distinct必须放在开头

使用where字句

select 列名称 from 表名称 where 条件表达式

关系运算符

select * from sc where 成绩>60
select 姓名,性别,班级 from student where left(姓名,1)='赵'
select 姓名,性别,班级 from student where year(出生日期)>1990
select sname from student where sdept !='CS'

逻辑关系符

select * from sc where not 成绩>=60
select * from student where 性别='男' and 班级='4'
select * from course where 考试类型='机考' or 学分=3

范围运算符

select name,dept,age from student where age between 20 and 23
select name,dept,age from student where age not between 20 and 23

列表运算符

select name,sex from student where dept in('IS','MA','CS')
select name,sex from student where dept not in('IS','MA','CS')

模糊匹配运算符

【not】like + ‘%’:表示0或多个字符

select name,no,sex from student where name like ‘刘%’
select name,no,sex from student where name not like ‘刘%’
select name,no,sex from student where name  like ‘%刚’
select name,no,sex from student where name  like ‘%小%’

【not】like + ‘_’:表示单个字符,一般用来限制字符长度

select name,no,sex from student where name  like ‘_小刚’
#以小刚结尾的三个字学生
select name,no,sex from student where name  like ‘_阳%’
#第二个字为阳的学生

【not】like + [ ]:表示范围内的单个字符

select * from user where name like '[张王李]三'
#找出所有叫张三李三王三的人的信息
select * from user where name like '老[1-9]'
#找出老1 老2 老3.....老9
#如果[]内有一系列字符(01234,abcdef)可以略写为[0-4],[a-f]
select * from persons where city like '[ALN]%'
#找出居住城市是以A 或者L或者 N开头的人的信息

【not】like + [^ ]:表示不在范围内的单个字符

select * from user where name like '[^张王李]三'
#找出不叫张三,王三,李三的使用者信息
select * from user where name like '老[^0-4]'
#找出不在老1-老4的使用者信息

空值运算符:is null / is not null

select sno,sco from student where grade is null
#查找缺少成绩学生的学号以及课程号
select sno,sco from student where grade is not null
#查找有成绩学生的学号以及课程号

多重条件查询

1.and优先级高于or
2.可以用括号改变优先级

select name from student where dept='CS' and age<20
select name,sex from student where dept='IS' or dept='CS' or dept='MA'

使用order by排序

对单个字段排序

asc(可省略),desc

select no,grade from sc where cno='3' order by grade desc
select * from student order by 出生日期 desc

对两个字段同时排序

select * from sc order by 学号,成绩 desc
#查询学生信息,学号升序排列,学号相同按照成绩降序排列

对空值字段进行排序

在这里插入图片描述
在这里插入图片描述

使用TOP关键字

返回确定的记录数目

select top 20 name from student
select top 20 * from student

返回指定百分比的记录数

select top 10 percent 姓名,出生日期 from student
select top 10 percent * from student 

top关键字结合where语句

select top 20 * from student where age>23

top关键字结合order by语句

select top 20 * from student order by age desc
select top 10 title,adddatetime from news order by adddatetime desc
select top 8 title,hits from news order by hits desc

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...