DQL数据查询语句使用示例

DQL(Data Query Language 数据查询语言):用于查询数据库对象中所包含的数据。DQL语言主要的语句:SELECT语句。DQL语言是数据库语言中最核心、最重要的语句,也是使用频率最高的语句

DQL

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

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

指定查询字段

-- 查询所有的学生  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.studentno select 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' ) ) 

到此这篇关于DQL数据查询语句使用示例的文章就介绍到这了,更多相关DQL数据查询内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是DQL数据查询语句使用示例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 数据库