DQL查询数据
创始人
2024-04-28 05:32:42
0

文章目录

  • DQL
  • 指定查询字段
  • where条件子句
  • 联表查询
  • 分页和排序
  • 子查询


DQL

(Data Query Language:数据查询语言)

  1. 所有的查询操作都要用到它 select
  2. 简单的查询,复杂的查询都要用到它
  3. 数据库最核心的语言,最重要的语言
  4. 使用频率最高的语言

指定查询字段

-- 查询所有的学生  select 字段 from 表
select * from student
-- 查询指定字段
select studentno,studentname from student
-- 给结果起一个名字 , As,可以给字段起别名,也可以给表起别名
select studentno as 学号,studentname as 学生姓名 from student
select studentno 学号,studentname 学生姓名 from student-- 函数 concat(a,b)
select CONCAT('姓名:',studentname) as 新名字 from student

语法:select 字段 from 表
有时候,列的名字不是那么的见名之意,我们可以用as给字段起别名
字段名 as 别名 或者是 表名 as 表别名

去重 distinct

作用:去除select查询出来的结果中重复的数据,重复的数据只显示一条

-- 查询一下有哪些同学参加了考试
select * from result -- 查询全部的考试成绩
select studentno from result -- 查询有哪些学生参加了考试
select distinct studentno from result -- 去重

数据库的列(表达式)

select version() -- 查询系统版本号(函数)
select 100*3-2 -- 用来计算(表达式)
select @@auto_increment_increment -- 查询自增的步长(变量)-- 学生考试成绩+1分查看
select studentno,studentresult+1 as '提分后' from result

数据库中的表达式:文本值,列,null,函数,计算表达式,系统变量

select 表达式 from 表

where条件子句

作用:检索符合条件的值

搜索的条件都是由一个或者多个表达式组成!结果都是布尔值

逻辑运算符

在这里插入图片描述

select studentno,studentresult from result-- 查询考试成绩在95~100之间的
-- and &&
select studentno,studentresult from result 
where studentresult>=95 and studentresult<=100
-- 模糊查询(区间)
select studentno,studentresult from result 
where studentresult between 95 and 100
-- ! not
select studentno,studentresult from result 
where not studentno=1000

模糊查询:比较运算符

在这里插入图片描述

-- ===========模糊查询========================
-- 查询姓张的同学
-- like结合%(代表0到任意个字符) _(一个字符)
select studentno,studentname from student
where studentname like '张%'
-- 查询姓张的同学,名字只有两个字
select studentno,studentname from student
where studentname like '张_'-- ======in(具体的一个或者多个值)========
-- 查询学号1000,1001的学生
select studentno,studentname from student
where studentno in(1000,1001)
-- 查询在北京的学生
select studentno,studentname from student
where address in('北京朝阳')-- ========null not null====================
-- 查询地址为空的同学 null或者''
select studentno,studentname from student
where address='' or address is null
-- 查询出生日期不为空的学生
select studentno,studentname from student
where borndate is not null

联表查询

在这里插入图片描述

 -- 查询参加考试的同学(学号,姓名,科目编号,分数)
select * from student
select * from result
/*思路:
1.分析需求:分析查询的字段来自哪些表(连接查询)
2.确定使用哪种连接查询? 7种确定交叉点,这两个表中哪些数据是相同的判断的条件:学生表的studentno=成绩表 studentno	
*/
select s.studentno,studentname,subjectno,studentresult 
from student as s
inner join  result as r
where s.studentno=r.studentno-- right join 
select s.studentno,studentname,subjectno,studentresult 
from student as s
right join result as r
on s.studentno = r.studentno-- left join 
select s.studentno,studentname,subjectno,studentresult 
from student as s
left join result as r
on s.studentno = r.studentno

在这里插入图片描述
练习:

-- ===============联表查询==================-- 查询参加考试的同学(学号,姓名,科目编号,分数)
select * from student
select * from result
/*思路:
1.分析需求:分析查询的字段来自哪些表(连接查询)
2.确定使用哪种连接查询? 7种确定交叉点,这两个表中哪些数据是相同的判断的条件:学生表的studentno=成绩表 studentno	
*/
-- join(连接的表) on(判断的条件) 连接查询
-- where 等值查询
select s.studentno,studentname,subjectno,studentresult 
from student as s
inner join  result as r
where s.studentno=r.studentnoselect s.studentno,studentname,subjectno,studentresult 
from student as s
inner join  result as r
on s.studentno=r.studentno-- right join 
select s.studentno,studentname,subjectno,studentresult 
from student as s
right join result as r
on r.studentno = s.studentno-- left join 
select s.studentno,studentname,subjectno,studentresult 
from student as s
left join result as r
on s.studentno = r.studentno-- 查询缺考的同学(只要让成绩为null,就能找到)
select s.studentno,studentname,subjectno,studentresult 
from student as s
left join result as r
on s.studentno = r.studentno where studentresult is null-- 查询了参加考试的同学信息:学号,学生姓名,科目名称,分数
-- 表 student result subject
-- 右查询
-- 交叉点:学生表 studentno=成绩表 studentno  
--         成绩表 subjectno=科目表 subjectno
select s.studentno,studentname,sub.subjectname,studentresult
from student as s
right join result as r
on r.studentno=s.studentno
inner join `subject` as sub
on r.subjectno=sub.subjectno
-- 我要查询哪些数据 select ...
-- 从哪几个表中查询 from 表 xxx join 连接的表 on 较差条件
-- 假设存在一种多张表查询,先查询两张表开始,然后再慢慢增加
-- from a left join b
-- from a right join b

自连接
自己的表和自己的表连接;

核心:一张表拆分成两张一样的表即可

父类表:
在这里插入图片描述
子类表
在这里插入图片描述查询父类对应的子类关系
在这里插入图片描述

-- 查询父子信息:把一张表看为两个一模一样的表
select a.categoryname as '父栏目',b.categoryname as '子栏目'
from category as a,category as b
where a.categoryid = b.pid

分页和排序

排序

-- 排序:升序 ASC  降序DESC
-- 通过哪个字段排序,怎么排
select s.studentno,studentname,subjectname,studentresult
from student as s
inner join result as r
on s.studentno = r.studentno
inner join `subject` as sub
on sub.subjectno=r.subjectno
where subjectname = '高等数学-1'
order by studentresult DESC

分页

-- 为什么要分页
-- 缓解数据库的压力,给人更好的体验  ---瀑布流
-- 分页,每页显示五条数据
-- 语法:limit 起始值,页面的大小
-- 网页应用:当前页,总页数,页面的大小
-- limit 0,5 1~5条数据
-- limit 1,5 2~6条数据select * from student limit 0,5
-- 第一页 limit 0,5   (1-1)*5
-- 第二页 limit 5,5   (2-1)*5
-- 第二页 limit 10,5  (3-1)*5
-- 第N页  limit       (n-1)*5
-- pageSize:页面的大小
-- (n-1)*pageSize:起始值
-- n:当前页
-- 数据总数/页面大小=总页数

语法:limit (查询起始下标,pageSize)

子查询

where (值是固定的,这个值是计算出来的)

本质:在where语句中嵌套一个子查询语句

-- 查询高等数学-1的所有考试结果(学号,科目编号,成绩),降序排列
-- 方式1
select studentno,subjectno,studentresult
from result as r
inner join `subject` as sub
on r.subjectno=sub.subjectno
where subjectname='高等数学-1'
order by studentresult DESC
-- 方式2:使用子查询
select studentno,subjectno,studentresult
from result
where subjectno=(select subjectno from `subject` where  subjectname='高等数学-1'
)order by studentresult DESC
-- 查询所有高等数学-1的学生的学号
select subjectno from `subject` where subjectname='高等数学-1'-- 查询分数不小于80分的学生的学号和姓名
select studentno,studentname
from student 
where studentno=(
select distinct studentno from result where studentresult>=80
)
-- 查询课程为高等数学-2并且分数不小于80分的同学的学号和姓名
-- 联表查询
select s.studentno,studentname
from student as s
inner join result as r
on s.studentno=r.studentno
inner join `subject` as sub
on r.subjectno=sub.subjectno
where subjectname='高等数学-2' and studentresult>=80
-- 子查询
select DISTINCT s.studentno,studentname
from student as s
inner join result as r
on s.studentno=r.studentno
where studentresult>=80 and subjectno=(select subjectno from `subject` where subjectname='高等数学-2'
)-- 子查询:(由里及外)
select studentno,studentname
from student where studentno in(select studentno from result where studentresult>=80 and subjectno=(select subjectno from `subject` where subjectname='高等数学-2')
)

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...