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

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...